home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / laserjet / dviplus.web (.txt) < prev    next >
Texinfo Document  |  1990-10-01  |  99KB  |  2,239 lines

  1. % DVIPLUS.WEB
  2. % This program is not copyrighted and may be used freely.
  3. % Written by Tor Lillqvist
  4. %            Technical Research Centre of Finland
  5. %            Lehtisaarentie 2 A
  6. %            SF-00340  HELSINKI
  7. %            FINLAND
  8. % E-mail: tml@@fingate.bitnet, tml@@santra.uucp, ...!mcvax!santra!tml
  9. % Phone: +358 0 4566132
  10. % Telex: 122972 vttha sf
  11. % Here is TeX material that gets inserted after \input webmac
  12. \def\hang{\hangindent 3em\indent\ignorespaces}
  13. \font\ninerm=cmr9
  14. \let\mc=\ninerm % medium caps for names like SAIL
  15. \def\PASCAL{Pascal}
  16. \def\(#1){} % this is used to make section names sort themselves better
  17. \def\9#1{} % this is used for sort keys in the index
  18. \def\title{DVI$\,$\lowercase{plus}}
  19. \def\contentspagenumber{1}
  20. \def\topofcontents{\null
  21.   \def\titlepage{F} % include headline on the contents page
  22.   \def\rheader{\mainfont\hfil \contentspagenumber}
  23.   \vfill
  24.   \centerline{\titlefont The {\ttitlefont DVIplus} processor}
  25.   \vskip 10pt
  26.   \centerline{\titlefont for HP LaserJet+}
  27.   \vskip 15pt
  28.   \centerline{(Version 1.2, April 1986)}
  29.   \vfill}
  30. \def\botofcontents{\vfill
  31.   \centerline{\baselineskip9pt
  32.     \vbox{\ninerm\noindent
  33.     `\TeX' is a
  34.     trademark of the American Mathematical Society.}}}
  35. \pageno=\contentspagenumber \advance\pageno by 1
  36. @* Introduction.
  37. The \.{DVIplus} program reads binary device-independent (``\.{DVI}'')
  38. files that are produced by document compilers such as \TeX,
  39. and translates them for printing on a HP LaserJet+
  40. page printer.
  41. This program is written by Tor Lillqvist,
  42. based on the DVItype program by Donald E.~Knuth.
  43. This version is for the \PASCAL/1000 compiler on the RTE--A operating
  44. system running on the HP1000 A--Series computers.
  45. Programs for
  46. typesetting need to be especially careful about how they do arithmetic; if
  47. rounding errors accumulate, margins won't be straight, vertical rules
  48. won't line up, and so on. But if rounding is done everywhere, even in the
  49. midst of words, there will be uneven spacing between the letters, and that
  50. looks bad. Human eyes notice differences of a thousandth of an inch in the
  51. positioning of lines that are close together; on low resolution devices,
  52. where rounding produces effects four times as great as this, the problem
  53. is especially critical. Experience has shown that unusual care is needed
  54. even on high-resolution equipment; for example, a mistake in the sixth
  55. significant hexadecimal place of a constant once led to a difficult-to-find
  56. bug in some software for the Alphatype CRS, which has a resolution of 5333
  57. pixels per inch (make that 5333.33333333 pixels per inch).  The document
  58. compilers that generate \.{DVI} files make certain assumptions about the
  59. arithmetic that will be used by \.{DVI}-reading software, and if these
  60. assumptions are violated the results will be of inferior quality.
  61. Therefore the present program is intended as a guide to proper procedure
  62. in the critical places where a bit of subtlety is involved.
  63. The |banner| string defined here should be changed whenever \.{DVIplus}
  64. gets modified.
  65. The editor of RTE--A automagically updates this timestamp when
  66. the file is written.
  67. @d banner=='This is DVIplus, RTE-A Version 1.2 <860605.2059>'
  68. @ This program is not written in standard \PASCAL, but
  69. the \PASCAL/1000 dialect used on HP1000 A--series computers
  70. running the RTE--A operating system;
  71. it should be easy to convert to other reasonable \PASCAL\
  72. dialects.
  73. Places where pecliarities depndent on the RTE--A implmentation have
  74. been used are listed in the index under ``system dependencies''.
  75. @!@^system dependencies@>
  76. One of the extensions to standard \PASCAL\ that we shall deal with is the
  77. ability to move to a random place in a binary file; another is to
  78. determine the length of a binary file.
  79. In fact, in RTE--A we cannot determine the logical length of a binary file,
  80. so another approach is used: The first doubleword in \.{PXL} and \.{DVI}
  81. files contains the size of the file (in doublewords = integers).
  82. The records are numbered sequentially starting from 1. The internal pointers
  83. in \.{DVI} and \.{PXL} files start the (byte) numbering from 0, so an offset
  84. is added in the |seek| calls.
  85. \PASCAL/1000 allows writing nonprinting characters to |text| files
  86. using a notation with a number sign followed by the decimal
  87. ordinal number of the character. For example, |write(f, #27'E')| writes
  88. an escape followed by a capital `E'.
  89. In \PASCAL/1000, output to |text| files is line buffered, and we cannot
  90. keep |write|ing without using |writeln| every now and then.
  91. This terminates the record, which
  92. normally causes a newline, but the terminal drivers in RTE--A
  93. leave the newline out if the last byte of a write request is underscore.
  94. This is handled by the |write_lj| macro.
  95. Another extension is to use a default |case| as in \.{TANGLE}, \.{WEAVE},
  96. @d othercases == otherwise {default for cases not listed explicitly}
  97. @d endcases == @+end {follows the default case in an extended |case| statement}
  98. @f othercases == else
  99. @f endcases == end
  100. @ Before the program heading we have some compiler options.
  101. These specify that code should be generated for the `CDS' mode,
  102. i.e. Code-and-Data-Separation.
  103. The binary input comes from |dvi_file| and the |pxl_file|s.
  104. @^system dependencies@>
  105. @=$Standard_Level 'HP1000', CDS On, Debug, Range Off$@>@/
  106. program DVIplus;
  107. type @<Types in the outer block@>@/
  108. var @<Globals in the outer block@>@/
  109. @<External routine declarations@>@/
  110. procedure initialize; {this procedure gets things started properly}
  111. var i:0..255;
  112. begin
  113.   @<Set initial values@>@/
  114. @ Here are some macros for common programming idioms.
  115. @d do_nothing == {empty statement}
  116. @d incr(#) == #:=#+1 {increase a variable by unity}
  117. @d decr(#) == #:=#-1 {decrease a variable by unity}
  118. @ We do our own error handling.
  119. @^system dependencies@>
  120. @d catch_errors==error_status:=true
  121. @d dont_catch_errors==error_status:=false
  122. @<Types...@>=
  123. @!error_type=(err_run,err_ema,err_io,err_fmp,err_seg,err_wrn);
  124. @!error_file_name=packed array [1..150] of char;
  125. @ The |error_status| flag is set |true| when errors are to be catched.
  126. If a file system related error occurs, |error_status| is set false.
  127. @<Glob...@>=
  128. @!error_status:boolean;
  129. @ If an error that we are not prepared to catch occurs, we call
  130. the standard error printing routine, and |halt|.
  131. @p procedure errorprinter @=$Alias 'Pas.ErrorPrinter'$@>
  132. (err_type: error_type; err_number, err_line: short;
  133. var err_file: error_file_name; err_flen: short);
  134.   external;@t\2@>@/
  135. procedure errorcatcher @=$Alias 'Pas.ErrorCatcher'$@>
  136. (err_type: error_type; err_number, err_line: short;
  137. var err_file: error_file_name; err_flen: short);
  138. begin
  139.   if err_type=err_wrn then
  140.      do_nothing
  141.   else if error_status and (err_type=err_fmp) then
  142.     error_status := false
  143.   else begin
  144.     errorprinter(err_type,err_number,err_line,err_file,err_flen);
  145.     halt(1); {an error we weren't prepared to catch}
  146.   end;
  147. @ External routines are declared here.
  148. |parameters| retrieves a runstring parameter.
  149. |LURQ| is used to lock the output device.
  150. |dcb_address| returns a pointer to the DCB (a sort of file control
  151. block for an open file.
  152. |fmp_interactive| tells if a DCB corresponds to an interactive device,
  153. and |fmp_lu| returns the `logical unit' number of a device.
  154. @^system dependencies@>
  155. @<External routine...@>=
  156. function parameters @=$Alias 'Pas.Parameters'$@>
  157.   (pos:short; var par:file_name; len:short):short; external; @t\2@>@#
  158. procedure LURQ(option:short; var lu:short; num: short; var key:short);
  159.   external; @t\2@>@#
  160. procedure dcb_address @=$Alias 'Pas.DcbAddress1'$@>
  161.   (var p:dcb_ptr; var f:text_file); external; @t\2@>@#
  162. function fmp_interactive @=$Alias 'FmpInteractive'$@>
  163.   (var d:dcb):short; external; @t\2@>@#
  164. function fmp_lu @=$Alias 'FmpLU'$@> (var d:dcb):short; external; @t\2@>@#
  165. @ Types related to system routines.
  166. @d name_length=64 {a file name shouldn't be longer than this}
  167. @<Types...@>=
  168. @!short=-32768..32767;
  169. @!file_name=packed array [1..name_length] of char;
  170. @!dcb=array [1..144] of short;
  171. @!dcb_ptr=^dcb;
  172. @ @<Globals...@>=
  173. @!laser_dcb:dcb_ptr;
  174. @!lu,@!key:short;
  175. @ We lock the printer after opening it if it is a device.
  176. This prevents intermixed output.
  177. The default output file name is given here.
  178. @^system dependencies@>
  179. @d default_out=='61'
  180. @<Open and lock |laser_file|@>=
  181. if parameters(arg_index,cur_name,name_length) <= 0 then
  182.   cur_name:=default_out;
  183. rewrite(laser_file,cur_name,'NOCCTL,SHARED');
  184. dcb_address(laser_dcb,laser_file);
  185. if fmp_interactive(laser_dcb^) <> 0 then begin
  186.   lu:=fmp_lu(laser_dcb^);
  187.   LURQ(1,lu,1,key);
  188. @ Labels (global and local).
  189. @d done=30 {go here when finished with a subtask}
  190. @d exit=999 {go here to leave a procedure}
  191. @d return==goto exit
  192. @ The following parameters can be changed to extend or
  193. reduce \.{DVIplus}'s capacity.
  194. @d max_fonts=75 {maximum number of distinct fonts per \.{DVI} file}
  195. @d max_printer_fonts=20 {maximum number of fonts kept in printer}
  196. @d max_fonts_on_page=16 {maximum number of fonts per page}
  197. @d max_widths=9601 {|max_fonts * 128 + 1|}
  198.   {maximum number of different characters among all fonts}
  199. @d line_length=79 {bracketed lines of output will be at most this long}
  200. @d terminal_line_length=128 {maximum number of characters input in a single
  201.   line of input from the terminal}
  202. @d stack_size=100 {\.{DVI} files shouldn't |push| beyond this depth}
  203. @d name_size=1000 {total length of all font file names}
  204. @d max_bops=1000 {maximum number of pages printed}
  205. @ If the \.{DVI} file is badly malformed, the whole process must be aborted;
  206. \.{DVIplus} will give up, after issuing an error message about the symptoms
  207. that were noticed.
  208. Such errors might be discovered inside of subroutines inside of subroutines,
  209. so a procedure called |jump_out| has been introduced. This procedure simply
  210. halts the program using the \PASCAL/1000 procedure |halt|.
  211. @^system dependencies@>
  212. @d print(#)==write(term_out,#)
  213. @d print_ln(#)==write_ln(term_out,#)
  214. @d print_nl==write_ln(term_out)
  215. @d abort(#)==begin print('? ',#); jump_out;
  216.     end
  217. @d bad_dvi(#)==abort('Bad DVI file: ',#,'!')
  218. @.Bad DVI file@>
  219. @d bad_pxl(#)==begin print_nl; abort('Bad PXL file: ',#,'!'); end
  220. @.Bad PXL file@>
  221. @p procedure jump_out;
  222. begin halt(1)
  223. @ We fill output lines to |laser_file|, breaking the line when
  224. |lj_threshold| characters have been written. An underscore character
  225. is appended to the line before the newline (record boundary).
  226. This prevents the RTE--A terminal driver from writing a {\tt CR~LF}
  227. pair at the end of the line.
  228. @^system dependencies@>
  229. All output to |laser_file| goes through the macros |write_lj| or
  230. |write_lj_f|. The latter doesn't test for line break before writing.
  231. @d lj_threshold=70
  232. @d write_lj(#)==begin if linepos(laser_file)>lj_threshold then
  233.     write_ln(laser_file,'_');
  234.   write(laser_file,#)
  235. @d write_lj_f(#)==write(laser_file,#)
  236. @* The character set.
  237. Like all programs written with the  \.{WEB} system, \.{DVIplus} can be
  238. used with any character set. But it uses ASCII code internally, because
  239. the programming for portable input-output is easier when a fixed internal
  240. code is used, and because \.{DVI} files use ASCII code for file names
  241. and certain other strings.
  242. The next few sections of \.{DVIplus} have therefore been copied from the
  243. analogous ones in the \.{WEB} system routines. They have been considerably
  244. simplified, since \.{DVIplus} need not deal with the controversial
  245. ASCII codes less than @'40. If such codes appear in the \.{DVI} file,
  246. they will be printed as question marks.
  247. @<Types...@>=
  248. @!ASCII_code=" ".."~"; {a subrange of the integers}
  249. @ The original \PASCAL\ compiler was designed in the late 60s, when six-bit
  250. character sets were common, so it did not make provision for lower case
  251. letters. Nowadays, of course, we need to deal with both upper and lower case
  252. alphabets in a convenient way, especially in a program like \.{DVIplus}.
  253. So we shall assume that the \PASCAL\ system being used for \.{DVIplus}
  254. has a character set containing at least the standard visible characters
  255. of ASCII code (|"!"| through |"~"|).
  256. Some \PASCAL\ compilers use the original name |char| for the data type
  257. associated with the characters in text files, while other \PASCAL s
  258. consider |char| to be a 64-element subrange of a larger data type that has
  259. some other name.  In order to accommodate this difference, we shall use
  260. the name |text_char| to stand for the data type of the characters in the
  261. output file.  We shall also assume that |text_char| consists of
  262. the elements |chr(first_text_char)| through |chr(last_text_char)|,
  263. inclusive. The following definitions should be adjusted if necessary.
  264. @^system dependencies@>
  265. @d text_char == char {the data type of characters in text files}
  266. @d first_text_char=0 {ordinal number of the smallest element of |text_char|}
  267. @d last_text_char=255 {ordinal number of the largest element of |text_char|}
  268. @<Types...@>=
  269. @!text_file=text_;
  270. @ The \.{DVIplus} processor converts between ASCII code and
  271. the user's external character set by means of arrays |xord| and |xchr|
  272. that are analogous to \PASCAL's |ord| and |chr| functions.
  273. @<Globals...@>=
  274. @!xord: array [text_char] of ASCII_code;
  275.   {specifies conversion of input characters}
  276. @!xchr: array [0..255] of text_char;
  277.   {specifies conversion of output characters}
  278. @ Under our assumption that the visible characters of standard ASCII are
  279. all present, the following assignment statements initialize the
  280. |xchr| array properly, without needing any system-dependent changes.
  281. @<Set init...@>=
  282. for i:=0 to @'37 do xchr[i]:='?';
  283. xchr[@'40]:=' ';
  284. xchr[@'41]:='!';
  285. xchr[@'42]:='"';
  286. xchr[@'43]:='#';
  287. xchr[@'44]:='$';
  288. xchr[@'45]:='%';
  289. xchr[@'46]:='&';
  290. xchr[@'47]:='''';@/
  291. xchr[@'50]:='(';
  292. xchr[@'51]:=')';
  293. xchr[@'52]:='*';
  294. xchr[@'53]:='+';
  295. xchr[@'54]:=',';
  296. xchr[@'55]:='-';
  297. xchr[@'56]:='.';
  298. xchr[@'57]:='/';@/
  299. xchr[@'60]:='0';
  300. xchr[@'61]:='1';
  301. xchr[@'62]:='2';
  302. xchr[@'63]:='3';
  303. xchr[@'64]:='4';
  304. xchr[@'65]:='5';
  305. xchr[@'66]:='6';
  306. xchr[@'67]:='7';@/
  307. xchr[@'70]:='8';
  308. xchr[@'71]:='9';
  309. xchr[@'72]:=':';
  310. xchr[@'73]:=';';
  311. xchr[@'74]:='<';
  312. xchr[@'75]:='=';
  313. xchr[@'76]:='>';
  314. xchr[@'77]:='?';@/
  315. xchr[@'100]:='@@';
  316. xchr[@'101]:='A';
  317. xchr[@'102]:='B';
  318. xchr[@'103]:='C';
  319. xchr[@'104]:='D';
  320. xchr[@'105]:='E';
  321. xchr[@'106]:='F';
  322. xchr[@'107]:='G';@/
  323. xchr[@'110]:='H';
  324. xchr[@'111]:='I';
  325. xchr[@'112]:='J';
  326. xchr[@'113]:='K';
  327. xchr[@'114]:='L';
  328. xchr[@'115]:='M';
  329. xchr[@'116]:='N';
  330. xchr[@'117]:='O';@/
  331. xchr[@'120]:='P';
  332. xchr[@'121]:='Q';
  333. xchr[@'122]:='R';
  334. xchr[@'123]:='S';
  335. xchr[@'124]:='T';
  336. xchr[@'125]:='U';
  337. xchr[@'126]:='V';
  338. xchr[@'127]:='W';@/
  339. xchr[@'130]:='X';
  340. xchr[@'131]:='Y';
  341. xchr[@'132]:='Z';
  342. xchr[@'133]:='[';
  343. xchr[@'134]:='\';
  344. xchr[@'135]:=']';
  345. xchr[@'136]:='^';
  346. xchr[@'137]:='_';@/
  347. xchr[@'140]:='`';
  348. xchr[@'141]:='a';
  349. xchr[@'142]:='b';
  350. xchr[@'143]:='c';
  351. xchr[@'144]:='d';
  352. xchr[@'145]:='e';
  353. xchr[@'146]:='f';
  354. xchr[@'147]:='g';@/
  355. xchr[@'150]:='h';
  356. xchr[@'151]:='i';
  357. xchr[@'152]:='j';
  358. xchr[@'153]:='k';
  359. xchr[@'154]:='l';
  360. xchr[@'155]:='m';
  361. xchr[@'156]:='n';
  362. xchr[@'157]:='o';@/
  363. xchr[@'160]:='p';
  364. xchr[@'161]:='q';
  365. xchr[@'162]:='r';
  366. xchr[@'163]:='s';
  367. xchr[@'164]:='t';
  368. xchr[@'165]:='u';
  369. xchr[@'166]:='v';
  370. xchr[@'167]:='w';@/
  371. xchr[@'170]:='x';
  372. xchr[@'171]:='y';
  373. xchr[@'172]:='z';
  374. xchr[@'173]:='{';
  375. xchr[@'174]:='|';
  376. xchr[@'175]:='}';
  377. xchr[@'176]:='~';
  378. for i:=@'177 to 255 do xchr[i]:='?';
  379. @ The following system-independent code makes the |xord| array contain a
  380. suitable inverse to the information in |xchr|.
  381. @<Set init...@>=
  382. for i:=first_text_char to last_text_char do xord[chr(i)]:=@'40;
  383. for i:=" " to "~" do xord[xchr[i]]:=i;
  384. @ The LaserJet refuses to print other that `visible characters', that is
  385. characters in the range @'41--@'177 or @'240--@'377.
  386. @p function vis_chr(p : integer):char;
  387. begin
  388.   if p < @'41 then
  389.     p:=p+@'240;
  390.   vis_chr:=chr(p);
  391. @* Device-independent file format.
  392. Before we get into the details of \.{DVIplus}, we need to know exactly
  393. what \.{DVI} files are. The form of such files was designed by David R.
  394. @^Fuchs, David Raymond@>
  395. Fuchs in 1979. Almost any reasonable typesetting device can be driven by
  396. a program that takes \.{DVI} files as input, and dozens of such
  397. \.{DVI}-to-whatever programs have been written. Thus, it is possible to
  398. print the output of document compilers like \TeX\ on many different kinds
  399. of equipment.
  400. A \.{DVI} file is a stream of 8-bit bytes, which may be regarded as a
  401. series of commands in a machine-like language. The first byte of each command
  402. is the operation code, and this code is followed by zero or more bytes
  403. that provide parameters to the command. The parameters themselves may consist
  404. of several consecutive bytes; for example, the `|set_rule|' command has two
  405. parameters, each of which is four bytes long. Parameters are usually
  406. regarded as nonnegative integers; but four-byte-long parameters,
  407. and shorter parameters that denote distances, can be
  408. either positive or negative. Such parameters are given in two's complement
  409. notation. For example, a two-byte-long distance parameter has a value between
  410. $-2^{15}$ and $2^{15}-1$.
  411. @.DVI {\rm files}@>
  412. A \.{DVI} file consists of a ``preamble,'' followed by a sequence of one
  413. or more ``pages,'' followed by a ``postamble.'' The preamble is simply a
  414. |pre| command, with its parameters that define the dimensions used in the
  415. file; this must come first.  Each ``page'' consists of a |bop| command,
  416. followed by any number of other commands that tell where characters are to
  417. be placed on a physical page, followed by an |eop| command. The pages
  418. appear in the order that they were generated, not in any particular
  419. numerical order. If we ignore |nop| commands and \\{fnt\_def} commands
  420. (which are allowed between any two commands in the file), each |eop|
  421. command is immediately followed by a |bop| command, or by a |post|
  422. command; in the latter case, there are no more pages in the file, and the
  423. remaining bytes form the postamble.  Further details about the postamble
  424. will be explained later.
  425. Some parameters in \.{DVI} commands are ``pointers.'' These are four-byte
  426. quantities that give the location number of some other byte in the file;
  427. the first byte is number~0, then comes number~1, and so on. For example,
  428. one of the parameters of a |bop| command points to the previous |bop|;
  429. this makes it feasible to read the pages in backwards order, in case the
  430. results are being directed to a device that stacks its output face up.
  431. Suppose the preamble of a \.{DVI} file occupies bytes 0 to 99. Now if the
  432. first page occupies bytes 100 to 999, say, and if the second
  433. page occupies bytes 1000 to 1999, then the |bop| that starts in byte 1000
  434. points to 100 and the |bop| that starts in byte 2000 points to 1000. (The
  435. very first |bop|, i.e., the one that starts in byte 100, has a pointer of $-1$.)
  436. @ The \.{DVI} format is intended to be both compact and easily interpreted
  437. by a machine. Compactness is achieved by making most of the information
  438. implicit instead of explicit. When a \.{DVI}-reading program reads the
  439. commands for a page, it keeps track of several quantities: (a)~The current
  440. font |f| is an integer; this value is changed only
  441. by \\{fnt} and \\{fnt\_num} commands. (b)~The current position on the page
  442. is given by two numbers called the horizontal and vertical coordinates,
  443. |h| and |v|. Both coordinates are zero at the upper left corner of the page;
  444. moving to the right corresponds to increasing the horizontal coordinate, and
  445. moving down corresponds to increasing the vertical coordinate. Thus, the
  446. coordinates are essentially Cartesian, except that vertical directions are
  447. flipped; the Cartesian version of |(h,v)| would be |(h,-v)|.  (c)~The
  448. current spacing amounts are given by four numbers |w|, |x|, |y|, and |z|,
  449. where |w| and~|x| are used for horizontal spacing and where |y| and~|z|
  450. are used for vertical spacing. (d)~There is a stack containing
  451. |(h,v,w,x,y,z)| values; the \.{DVI} commands |push| and |pop| are used to
  452. change the current level of operation. Note that the current font~|f| is
  453. not pushed and popped; the stack contains only information about
  454. positioning.
  455. The values of |h|, |v|, |w|, |x|, |y|, and |z| are signed integers having up
  456. to 32 bits, including the sign. Since they represent physical distances,
  457. there is a small unit of measurement such that increasing |h| by~1 means
  458. moving a certain tiny distance to the right. The actual unit of
  459. measurement is variable, as explained below.
  460. @ Here is a list of all the commands that may appear in a \.{DVI} file. Each
  461. command is specified by its symbolic name (e.g., |bop|), its opcode byte
  462. (e.g., 139), and its parameters (if any). The parameters are followed
  463. by a bracketed number telling how many bytes they occupy; for example,
  464. `|p[4]|' means that parameter |p| is four bytes long.
  465. \yskip\hang|set_char_0| 0. Typeset character number~0 from font~|f|
  466. such that the reference point of the character is at |(h,v)|. Then
  467. increase |h| by the width of that character. Note that a character may
  468. have zero or negative width, so one cannot be sure that |h| will advance
  469. after this command; but |h| usually does increase.
  470. \yskip\hang|set_char_1| through |set_char_127| (opcodes 1 to 127).
  471. Do the operations of |set_char_0|; but use the character whose number
  472. matches the opcode, instead of character~0.
  473. \yskip\hang|set1| 128 |c[1]|. Same as |set_char_0|, except that character
  474. number~|c| is typeset. \TeX82 uses this command for characters in the
  475. range |128<=c<256|.
  476. \yskip\hang|set2| 129 |c[2]|. Same as |set1|, except that |c|~is two
  477. bytes long, so it is in the range |0<=c<65536|. \TeX82 never uses this
  478. command, which is intended for processors that deal with oriental languages;
  479. but \.{DVIplus} will allow character codes greater than 255, assuming that
  480. they all have the same width as the character whose code is $c \bmod 256$.
  481. @^oriental characters@>@^Chinese characters@>@^Japanese characters@>
  482. \yskip\hang|set3| 130 |c[3]|. Same as |set1|, except that |c|~is three
  483. bytes long, so it can be as large as $2^{24}-1$.
  484. \yskip\hang|set4| 131 |c[4]|. Same as |set1|, except that |c|~is four
  485. bytes long, possibly even negative. Imagine that.
  486. \yskip\hang|set_rule| 132 |a[4]| |b[4]|. Typeset a solid black rectangle
  487. of height |a| and width |b|, with its bottom left corner at |(h,v)|. Then
  488. set |h:=h+b|. If either |a<=0| or |b<=0|, nothing should be typeset. Note
  489. that if |b<0|, the value of |h| will decrease even though nothing else happens.
  490. Programs that typeset from \.{DVI} files should be careful to make the rules
  491. line up carefully with digitized characters, as explained in connection with
  492. the |rule_pixels| subroutine below.
  493. \yskip\hang|put1| 133 |c[1]|. Typeset character number~|c| from font~|f|
  494. such that the reference point of the character is at |(h,v)|. (The `put'
  495. commands are exactly like the `set' commands, except that they simply put out a
  496. character or a rule without moving the reference point afterwards.)
  497. \yskip\hang|put2| 134 |c[2]|. Same as |set2|, except that |h| is not changed.
  498. \yskip\hang|put3| 135 |c[3]|. Same as |set3|, except that |h| is not changed.
  499. \yskip\hang|put4| 136 |c[4]|. Same as |set4|, except that |h| is not changed.
  500. \yskip\hang|put_rule| 137 |a[4]| |b[4]|. Same as |set_rule|, except that
  501. |h| is not changed.
  502. \yskip\hang|nop| 138. No operation, do nothing. Any number of |nop|'s
  503. may occur between \.{DVI} commands, but a |nop| cannot be inserted between
  504. a command and its parameters or between two parameters.
  505. \yskip\hang|bop| 139 $c_0[4]$ $c_1[4]$ $\ldots$ $c_9[4]$ $p[4]$. Beginning
  506. of a page: Set |(h,v,w,x,y,z):=(0,0,0,0,0,0)| and set the stack empty. Set
  507. the current font |f| to an undefined value.  The ten $c_i$ parameters can
  508. be used to identify pages, if a user wants to print only part of a \.{DVI}
  509. file; \TeX82 gives them the values of \.{\\count0} $\ldots$ \.{\\count9}
  510. at the time \.{\\shipout} was invoked for this page.  The parameter |p|
  511. points to the previous |bop| command in the file, where the first |bop|
  512. has $p=-1$.
  513. \yskip\hang|eop| 140.  End of page: Print what you have read since the
  514. previous |bop|. At this point the stack should be empty. (The \.{DVI}-reading
  515. programs that drive most output devices will have kept a buffer of the
  516. material that appears on the page that has just ended. This material is
  517. largely, but not entirely, in order by |v| coordinate and (for fixed |v|) by
  518. |h|~coordinate; so it usually needs to be sorted into some order that is
  519. appropriate for the device in question. \.{DVIplus} does not do such sorting.)
  520. \yskip\hang|push| 141. Push the current values of |(h,v,w,x,y,z)| onto the
  521. top of the stack; do not change any of these values. Note that |f| is
  522. not pushed.
  523. \yskip\hang|pop| 142. Pop the top six values off of the stack and assign
  524. them to |(h,v,w,x,y,z)|. The number of pops should never exceed the number
  525. of pushes, since it would be highly embarrassing if the stack were empty
  526. at the time of a |pop| command.
  527. \yskip\hang|right1| 143 |b[1]|. Set |h:=h+b|, i.e., move right |b| units.
  528. The parameter is a signed number in two's complement notation, |-128<=b<128|;
  529. if |b<0|, the reference point actually moves left.
  530. \yskip\hang|right2| 144 |b[2]|. Same as |right1|, except that |b| is a
  531. two-byte quantity in the range |-32768<=b<32768|.
  532. \yskip\hang|right3| 145 |b[3]|. Same as |right1|, except that |b| is a
  533. three-byte quantity in the range |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
  534. \yskip\hang|right4| 146 |b[4]|. Same as |right1|, except that |b| is a
  535. four-byte quantity in the range |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
  536. \yskip\hang|w0| 147. Set |h:=h+w|; i.e., move right |w| units. With luck,
  537. this parameterless command will usually suffice, because the same kind of motion
  538. will occur several times in succession; the following commands explain how
  539. |w| gets particular values.
  540. \yskip\hang|w1| 148 |b[1]|. Set |w:=b| and |h:=h+b|. The value of |b| is a
  541. signed quantity in two's complement notation, |-128<=b<128|. This command
  542. changes the current |w|~spacing and moves right by |b|.
  543. \yskip\hang|w2| 149 |b[2]|. Same as |w1|, but |b| is a two-byte-long
  544. parameter, |-32768<=b<32768|.
  545. \yskip\hang|w3| 150 |b[3]|. Same as |w1|, but |b| is a three-byte-long
  546. parameter, |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
  547. \yskip\hang|w4| 151 |b[4]|. Same as |w1|, but |b| is a four-byte-long
  548. parameter, |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
  549. \yskip\hang|x0| 152. Set |h:=h+x|; i.e., move right |x| units. The `|x|'
  550. commands are like the `|w|' commands except that they involve |x| instead
  551. of |w|.
  552. \yskip\hang|x1| 153 |b[1]|. Set |x:=b| and |h:=h+b|. The value of |b| is a
  553. signed quantity in two's complement notation, |-128<=b<128|. This command
  554. changes the current |x|~spacing and moves right by |b|.
  555. \yskip\hang|x2| 154 |b[2]|. Same as |x1|, but |b| is a two-byte-long
  556. parameter, |-32768<=b<32768|.
  557. \yskip\hang|x3| 155 |b[3]|. Same as |x1|, but |b| is a three-byte-long
  558. parameter, |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
  559. \yskip\hang|x4| 156 |b[4]|. Same as |x1|, but |b| is a four-byte-long
  560. parameter, |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
  561. \yskip\hang|down1| 157 |a[1]|. Set |v:=v+a|, i.e., move down |a| units.
  562. The parameter is a signed number in two's complement notation, |-128<=a<128|;
  563. if |a<0|, the reference point actually moves up.
  564. \yskip\hang|down2| 158 |a[2]|. Same as |down1|, except that |a| is a
  565. two-byte quantity in the range |-32768<=a<32768|.
  566. \yskip\hang|down3| 159 |a[3]|. Same as |down1|, except that |a| is a
  567. three-byte quantity in the range |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
  568. \yskip\hang|down4| 160 |a[4]|. Same as |down1|, except that |a| is a
  569. four-byte quantity in the range |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
  570. \yskip\hang|y0| 161. Set |v:=v+y|; i.e., move down |y| units. With luck,
  571. this parameterless command will usually suffice, because the same kind of motion
  572. will occur several times in succession; the following commands explain how
  573. |y| gets particular values.
  574. \yskip\hang|y1| 162 |a[1]|. Set |y:=a| and |v:=v+a|. The value of |a| is a
  575. signed quantity in two's complement notation, |-128<=a<128|. This command
  576. changes the current |y|~spacing and moves down by |a|.
  577. \yskip\hang|y2| 163 |a[2]|. Same as |y1|, but |a| is a two-byte-long
  578. parameter, |-32768<=a<32768|.
  579. \yskip\hang|y3| 164 |a[3]|. Same as |y1|, but |a| is a three-byte-long
  580. parameter, |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
  581. \yskip\hang|y4| 165 |a[4]|. Same as |y1|, but |a| is a four-byte-long
  582. parameter, |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
  583. \yskip\hang|z0| 166. Set |v:=v+z|; i.e., move down |z| units. The `|z|' commands
  584. are like the `|y|' commands except that they involve |z| instead of |y|.
  585. \yskip\hang|z1| 167 |a[1]|. Set |z:=a| and |v:=v+a|. The value of |a| is a
  586. signed quantity in two's complement notation, |-128<=a<128|. This command
  587. changes the current |z|~spacing and moves down by |a|.
  588. \yskip\hang|z2| 168 |a[2]|. Same as |z1|, but |a| is a two-byte-long
  589. parameter, |-32768<=a<32768|.
  590. \yskip\hang|z3| 169 |a[3]|. Same as |z1|, but |a| is a three-byte-long
  591. parameter, |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
  592. \yskip\hang|z4| 170 |a[4]|. Same as |z1|, but |a| is a four-byte-long
  593. parameter, |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
  594. \yskip\hang|fnt_num_0| 171. Set |f:=0|. Font 0 must previously have been
  595. defined by a \\{fnt\_def} instruction, as explained below.
  596. \yskip\hang|fnt_num_1| through |fnt_num_63| (opcodes 172 to 234). Set
  597. |f:=1|, \dots, |f:=63|, respectively.
  598. \yskip\hang|fnt1| 235 |k[1]|. Set |f:=k|. \TeX82 uses this command for font
  599. numbers in the range |64<=k<256|.
  600. \yskip\hang|fnt2| 236 |k[2]|. Same as |fnt1|, except that |k|~is two
  601. bytes long, so it is in the range |0<=k<65536|. \TeX82 never generates this
  602. command, but large font numbers may prove useful for specifications of
  603. color or texture, or they may be used for special fonts that have fixed
  604. numbers in some external coding scheme.
  605. \yskip\hang|fnt3| 237 |k[3]|. Same as |fnt1|, except that |k|~is three
  606. bytes long, so it can be as large as $2^{24}-1$.
  607. \yskip\hang|fnt4| 238 |k[4]|. Same as |fnt1|, except that |k|~is four
  608. bytes long; this is for the really big font numbers (and for the negative ones).
  609. \yskip\hang|xxx1| 239 |k[1]| |x[k]|. This command is undefined in
  610. general; it functions as a $(k+2)$-byte |nop| unless special \.{DVI}-reading
  611. programs are being used. \TeX82 generates |xxx1| when a short enough
  612. \.{\\special} appears, setting |k| to the number of bytes being sent. It
  613. is recommended that |x| be a string having the form of a keyword followed
  614. by possible parameters relevant to that keyword.
  615. \yskip\hang|xxx2| 240 |k[2]| |x[k]|. Like |xxx1|, but |0<=k<65536|.
  616. \yskip\hang|xxx3| 241 |k[3]| |x[k]|. Like |xxx1|, but |0<=k<@t$2^{24}$@>|.
  617. \yskip\hang|xxx4| 242 |k[4]| |x[k]|. Like |xxx1|, but |k| can be ridiculously
  618. large. \TeX82 uses |xxx4| when |xxx1| would be incorrect.
  619. \yskip\hang|fnt_def1| 243 |k[1]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  620. Define font |k|, where |0<=k<256|; font definitions will be explained shortly.
  621. \yskip\hang|fnt_def2| 244 |k[2]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  622. Define font |k|, where |0<=k<65536|.
  623. \yskip\hang|fnt_def3| 245 |k[3]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  624. Define font |k|, where |0<=k<@t$2^{24}$@>|.
  625. \yskip\hang|fnt_def4| 246 |k[4]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  626. Define font |k|, where |@t$-2^{31}$@><=k<@t$2^{31}$@>|.
  627. \yskip\hang|pre| 247 |i[1]| |num[4]| |den[4]| |mag[4]| |k[1]| |x[k]|.
  628. Beginning of the preamble; this must come at the very beginning of the
  629. file. Parameters |i|, |num|, |den|, |mag|, |k|, and |x| are explained below.
  630. \yskip\hang|post| 248. Beginning of the postamble, see below.
  631. \yskip\hang|post_post| 249. Ending of the postamble, see below.
  632. \yskip\noindent Commands 250--255 are undefined at the present time.
  633. @ @d set_char_0=0 {typeset character 0 and move right}
  634. @d set1=128 {typeset a character and move right}
  635. @d set_rule=132 {typeset a rule and move right}
  636. @d put1=133 {typeset a character}
  637. @d put_rule=137 {typeset a rule}
  638. @d nop=138 {no operation}
  639. @d bop=139 {beginning of page}
  640. @d eop=140 {ending of page}
  641. @d push=141 {save the current positions}
  642. @d pop=142 {restore previous positions}
  643. @d right1=143 {move right}
  644. @d w0=147 {move right by |w|}
  645. @d w1=148 {move right and set |w|}
  646. @d x0=152 {move right by |x|}
  647. @d x1=153 {move right and set |x|}
  648. @d down1=157 {move down}
  649. @d y0=161 {move down by |y|}
  650. @d y1=162 {move down and set |y|}
  651. @d z0=166 {move down by |z|}
  652. @d z1=167 {move down and set |z|}
  653. @d fnt_num_0=171 {set current font to 0}
  654. @d fnt1=235 {set current font}
  655. @d xxx1=239 {extension to \.{DVI} primitives}
  656. @d xxx4=242 {potentially long extension to \.{DVI} primitives}
  657. @d fnt_def1=243 {define the meaning of a font number}
  658. @d pre=247 {preamble}
  659. @d post=248 {postamble beginning}
  660. @d post_post=249 {postamble ending}
  661. @d undefined_commands==250,251,252,253,254,255
  662. @ The preamble contains basic information about the file as a whole. As
  663. stated above, there are six parameters:
  664. $$\hbox{|@!i[1]| |@!num[4]| |@!den[4]| |@!mag[4]| |@!k[1]| |@!x[k]|.}$$
  665. The |i| byte identifies \.{DVI} format; currently this byte is always set
  666. to~2. (Some day we will set |i=3|, when \.{DVI} format makes another
  667. incompatible change---perhaps in 1992.)
  668. The next two parameters, |num| and |den|, are positive integers that define
  669. the units of measurement; they are the numerator and denominator of a
  670. fraction by which all dimensions in the \.{DVI} file could be multiplied
  671. in order to get lengths in units of $10^{-7}$ meters. (For example, there are
  672. exactly 7227 \TeX\ points in 254 centimeters, and \TeX82 works with scaled
  673. points where there are $2^{16}$ sp in a point, so \TeX82 sets |num=25400000|
  674. and $|den|=7227\cdot2^{16}=473628672$.)
  675. @^sp@>
  676. The |mag| parameter is what \TeX82 calls \.{\\mag}, i.e., 1000 times the
  677. desired magnification. The actual fraction by which dimensions are
  678. multiplied is therefore $mn/1000d$. Note that if a \TeX\ source document
  679. does not call for any `\.{true}' dimensions, and if you change it only by
  680. specifying a different \.{\\mag} setting, the \.{DVI} file that \TeX\
  681. creates will be completely unchanged except for the value of |mag| in the
  682. preamble and postamble. (Fancy \.{DVI}-reading programs allow users to
  683. override the |mag|~setting when a \.{DVI} file is being printed.)
  684. Finally, |k| and |x| allow the \.{DVI} writer to include a comment, which is not
  685. interpreted further. The length of comment |x| is |k|, where |0<=k<256|.
  686. @d dvi_id=2 {identifies the kind of \.{DVI} files described here}
  687. @ Font definitions for a given font number |k| contain further parameters
  688. $$\hbox{|c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.}$$
  689. The four-byte value |c| is the check sum that \TeX\ (or whatever program
  690. generated the \.{DVI} file) found in the \.{TFM} file for this font;
  691. |c| should match the check sum of the font found by programs that read
  692. this \.{DVI} file.
  693. @^check sum@>
  694. Parameter |s| contains a fixed-point scale factor that is applied to the
  695. character widths in font |k|; font dimensions in \.{TFM} files and other
  696. font files are relative to this quantity, which is always positive and
  697. less than $2^{27}$. It is given in the same units as the other dimensions
  698. of the \.{DVI} file.  Parameter |d| is similar to |s|; it is the ``design
  699. size,'' and it is given in \.{DVI} units that have not been corrected for
  700. the magnification~|mag| found in the preamble.  Thus, font |k| is to be
  701. used at $|mag|\cdot s/1000d$ times its normal size.
  702. The remaining part of a font definition gives the external name of the font,
  703. which is an ASCII string of length |a+l|. The number |a| is the length
  704. of the ``area'' or directory, and |l| is the length of the font name itself;
  705. the standard local system font area is supposed to be used when |a=0|.
  706. The |n| field contains the area in its first |a| bytes.
  707. Font definitions must appear before the first use of a particular font number.
  708. Once font |k| is defined, it must not be defined again; however, we
  709. shall see below that font definitions appear in the postamble as well as
  710. in the pages, so in this sense each font number is defined exactly twice,
  711. if at all. Like |nop| commands and \\{xxx} commands, font definitions can
  712. appear before the first |bop|, or between an |eop| and a |bop|.
  713. @ The last page in a \.{DVI} file is followed by `|post|'; this command
  714. introduces the postamble, which summarizes important facts that \TeX\ has
  715. accumulated about the file, making it possible to print subsets of the data
  716. with reasonable efficiency. The postamble has the form
  717. $$\vbox{\halign{\hbox{#\hfil}\cr
  718.   |post| |p[4]| |num[4]| |den[4]| |mag[4]| |l[4]| |u[4]| |s[2]| |t[2]|\cr
  719.   $\langle\,$font definitions$\,\rangle$\cr
  720.   |post_post| |q[4]| |i[1]| 223's$[{\G}4]$\cr}}$$
  721. Here |p| is a pointer to the final |bop| in the file. The next three
  722. parameters, |num|, |den|, and |mag|, are duplicates of the quantities that
  723. appeared in the preamble.
  724. Parameters |l| and |u| give respectively the height-plus-depth of the tallest
  725. page and the width of the widest page, in the same units as other dimensions
  726. of the file. These numbers might be used by a \.{DVI}-reading program to
  727. position individual ``pages'' on large sheets of film or paper.
  728. Parameter |s| is the maximum stack depth (i.e., the largest excess of
  729. |push| commands over |pop| commands) needed to process this file. Then
  730. comes |t|, the total number of pages (|bop| commands) present.
  731. The postamble continues with font definitions, which are any number of
  732. \\{fnt\_def} commands as described above, possibly interspersed with |nop|
  733. commands. Each font number that is used in the \.{DVI} file must be defined
  734. exactly twice: Once before it is first selected by a \\{fnt} command, and once
  735. in the postamble.
  736. @ The last part of the postamble, following the |post_post| byte that
  737. signifies the end of the font definitions, contains |q|, a pointer to the
  738. |post| command that started the postamble.  An identification byte, |i|,
  739. comes next; this currently equals~2, as in the preamble.
  740. The |i| byte is followed by four or more bytes that are all equal to
  741. the decimal number 223 (i.e., @'337 in octal). \TeX\ puts out four to seven of
  742. these trailing bytes, until the total length of the file is a multiple of
  743. four bytes, since this works out best on machines that pack four bytes per
  744. word; but any number of 223's is allowed, as long as there are at least four
  745. of them. In effect, 223 is a sort of signature that is added at the very end.
  746. @^Fuchs, David Raymond@>
  747. This curious way to finish off a \.{DVI} file makes it feasible for
  748. \.{DVI}-reading programs to find the postamble first, on most computers,
  749. even though \TeX\ wants to write the postamble last. Most operating
  750. systems permit random access to individual words or bytes of a file, so
  751. the \.{DVI} reader can start at the end and skip backwards over the 223's
  752. until finding the identification byte. Then it can back up four bytes, read
  753. |q|, and move to byte |q| of the file. This byte should, of course,
  754. contain the value 248 (|post|); now the postamble can be read, so the
  755. \.{DVI} reader discovers all the information needed for typesetting the
  756. pages. Note that it is also possible to skip through the \.{DVI} file at
  757. reasonably high speed to locate a particular page, if that proves
  758. desirable. This saves a lot of time, since \.{DVI} files used in production
  759. jobs tend to be large.
  760. @* Input from binary files.
  761. We have seen that a \.{DVI} file is a sequence of 8-bit bytes. The bytes
  762. appear physically in what is called a `|packed file of 0..255|'
  763. in \PASCAL\ lingo.
  764. Packing is system dependent, and many \PASCAL\ systems fail to implement
  765. such files in a sensible way (at least, from the viewpoint of producing
  766. good production software).  For example, some systems treat all
  767. byte-oriented files as text, looking for end-of-line marks and such
  768. things. Therefore some system-dependent code is often needed to deal with
  769. binary files.
  770. @^system dependencies@>
  771. In the RTE--A implementation, \.{DVI} and \.{PXL} files are files of
  772. |integer|s,
  773. with the first |integer| containing the number of |integer|s to follow.
  774. (It is impossible to know the logical size of a random-access file).
  775. The bytes of the file are accessed through a variant record.
  776. @d pxl_id=1001 {identifies the kind of \.{PXL} files handled}
  777. @<Types...@>=
  778. @!eight_bits=0..255; {unsigned one-byte quantity}
  779. four=0..3;
  780. @!i2c=packed record case four of 0: (i:integer);@/
  781. 1:(i0,i1:short);@/
  782. 2:(b:packed array[0..3] of eight_bits);
  783. 3:(c:packed array[0..3] of char) end;
  784. @!int_file=file of integer;
  785. @!byte_file=record {files that contain binary data}
  786. x:i2c;i:0..3;m,s:integer;
  787. f:int_file;
  788. @ The program deals with the binary file variables |dvi_file| which is the
  789. main input file that we are printing, and |pxl_file| is an array of
  790. font image files from which character images is downloaded
  791. to the printer.
  792. @<Glob...@>=
  793. @!laser_file:text_file;
  794. @!dvi_file:byte_file; {the stuff we are \.{DVI}typing}
  795. @!pxl_file:array[0..max_fonts] of file of i2c; {font image files}
  796. @!s_pxl_file:integer; {size of |pxl_file|}
  797. @ To prepare these files for input, we |reset| them. An extension of
  798. \PASCAL\ is needed in the case of |pxl_file|, since we want to associate
  799. it with external files whose names are specified dynamically (i.e., not
  800. known at compile time). The following code assumes that `|reset(f,s)|'
  801. does this, when |f| is a file variable and |s| is a string variable that
  802. specifies the file name.
  803. The function result is false if the file couldn't be opened.
  804. @^system dependencies@>
  805. @p function open_dvi_file:boolean;
  806. var i,j:short;
  807. begin
  808. i:=parameters(arg_index,cur_name,name_length);
  809. incr(arg_index);
  810. j:=1;
  811. while (j<=i)and(cur_name[j]<>'.') do incr(j);
  812. if j>i then begin
  813.   cur_name[j]:='.'; incr(j);
  814.   cur_name[j]:='d'; incr(j);
  815.   cur_name[j]:='v'; incr(j);
  816.   cur_name[j]:='i'; incr(j);
  817. catch_errors;
  818. reset(dvi_file.f,cur_name,'SHARED');
  819. open_dvi_file:=error_status;
  820. if error_status then begin
  821.   dont_catch_errors;
  822.   close(dvi_file.f);
  823.   open(dvi_file.f,cur_name,'SHARED');
  824.   get(dvi_file.f);
  825.   dvi_file.s:=dvi_file.f^;
  826.   get(dvi_file.f);
  827.   dvi_file.x.i:=dvi_file.f^;
  828.   dvi_file.i:=0;
  829. cur_loc:=0;
  830. function open_pxl_file(i:short):boolean;
  831. begin
  832. catch_errors;
  833. reset(pxl_file[i],cur_name,'SHARED');
  834. open_pxl_file:=error_status;
  835. if error_status then begin
  836.   dont_catch_errors;
  837.   close(pxl_file[i]);
  838.   open(pxl_file[i],cur_name,'SHARED');
  839.   get(pxl_file[i]);
  840.   s_pxl_file:=pxl_file[i]^.i;
  841.   get(pxl_file[i]);
  842.   if pxl_file[i]^.i <> pxl_id then
  843.     bad_pxl('bad header id');
  844. @ If you looked carefully at the preceding code, you probably asked,
  845. ``What are |cur_loc| and |cur_name|?'' Good question. They're global
  846. variables: |cur_loc| is the number of the byte about to be read next from
  847. |dvi_file|, and |cur_name| is a string variable that will be set to the
  848. current pixel image file name before |open_pxl_file| is called.
  849. @<Glob...@>=
  850. @!cur_loc:integer; {where we are about to look, in |dvi_file|}
  851. @!cur_name:file_name; {external name,
  852.   with no lower case letters}
  853. @ It turns out to be convenient to read four bytes at a time, when we are
  854. inputting from \.{PXL} files. The input goes into global variables
  855. |b0|, |b1|, |b2|, and |b3|, with |b0| getting the first byte and |b3|
  856. the fourth.
  857. @<Glob...@>=
  858. @!b0,@!b1,@!b2,@!b3: integer; {four bytes input at once}
  859. @ The |read_pxl_word| procedure sets |b0| through |b3| to the next
  860. four bytes in the current \.{TFM} file.
  861. @^system dependencies@>
  862. @p procedure read_pxl_word(i:short);
  863. begin
  864. b0:=pxl_file[i]^.b[0];b1:=pxl_file[i]^.b[1];
  865. b2:=pxl_file[i]^.b[2];b3:=pxl_file[i]^.b[3];
  866. get(pxl_file[i]);
  867. @ We shall use another set of simple functions to read the next byte or
  868. bytes from |dvi_file|. There are seven possibilities, each of which is
  869. treated as a separate function in order to minimize the overhead for
  870. subroutine calls.
  871. @^system dependencies@>
  872. This is the best way to check ``eof'' in RTE--A, as the logical length of
  873. the file is unknown (except from the first word).
  874. @d eof_dvi_file==(cur_loc>dvi_file.s*4)
  875. @p function get_byte:integer; {returns the next byte, unsigned}
  876. var b:eight_bits;
  877. begin if eof_dvi_file then get_byte:=0
  878. else  begin
  879.     get_byte:=dvi_file.x.b[dvi_file.i];
  880.     if dvi_file.i=3 then begin
  881.         get(dvi_file.f);
  882.         dvi_file.x.i:=dvi_file.f^;
  883.         dvi_file.i:=0; end
  884.     else
  885.         incr(dvi_file.i);
  886.   end;
  887.   incr(cur_loc);
  888. function signed_byte:integer; {returns the next byte, signed}
  889. var b:eight_bits;
  890. begin b:=get_byte;
  891. if b<128 then signed_byte:=b @+ else signed_byte:=b-256;
  892. function get_two_bytes:integer; {returns the next two bytes, unsigned}
  893. var a,@!b:integer;
  894. begin a:=get_byte; b:=get_byte;
  895. get_two_bytes:=a*256+b;
  896. function signed_pair:integer; {returns the next two bytes, signed}
  897. var a,@!b:integer;
  898. begin a:=get_byte; b:=get_byte;
  899. if a<128 then signed_pair:=a*256+b
  900. else signed_pair:=(a-256)*256+b;
  901. function get_three_bytes:integer; {returns the next three bytes, unsigned}
  902. var a,@!b,@!c:eight_bits;
  903. begin a:=get_byte; b:=get_byte; c:=get_byte;
  904. get_three_bytes:=(a*256+b)*256+c;
  905. function signed_trio:integer; {returns the next three bytes, signed}
  906. var a,@!b,@!c:integer;
  907. begin a:=get_byte; b:=get_byte; c:=get_byte;
  908. if a<128 then signed_trio:=(a*256+b)*256+c
  909. else signed_trio:=((a-256)*256+b)*256+c;
  910. function signed_quad:integer; {returns the next four bytes, signed}
  911. var a,@!b,@!c,@!d:integer;
  912. begin a:=get_byte; b:=get_byte; c:=get_byte; d:=get_byte;
  913. if a<128 then signed_quad:=((a*256+b)*256+c)*256+d
  914. else signed_quad:=(((a-256)*256+b)*256+c)*256+d;
  915. @ Finally we come to the routines that are used to acces the |dvi_file|
  916. randomly. The driver program below needs two such routines: |dvi_length|
  917. should compute the total number of bytes in |dvi_file|, possibly also
  918. causing |eof_dvi_file| to be true; and |move_to_byte(n)|
  919. should position |dvi_file| so that the next |get_byte| will read byte |n|,
  920. starting with |n=0| for the first byte in the file.
  921. @^system dependencies@>
  922. Such routines are, of course, highly system dependent.
  923. @d dvi_length==(dvi_file.s*4)
  924. @d move_to_byte(#)==begin seek(dvi_file.f,((#) div 4)+2); get(dvi_file.f);
  925. dvi_file.x.i:=dvi_file.f^; dvi_file.i:=(#) mod 4; cur_loc:=#; end
  926. Font files
  927. should contain exactly the same character width data that is
  928. found in the corresponding \.{TFM}s; check sums are used to help
  929. ensure this. In addition, font files also contain the widths of
  930. characters in pixels, since the device-independent character widths of
  931. \.{TFM} files are generally not perfect multiples of pixels.
  932. The |pixel_width| array contains this information; when |width[k]| is the
  933. device-independent width of some character in \.{DVI} units, |pixel_width[k]|
  934. is the corresponding width of that character in an actual font.
  935. The macro |char_pixel_width| is set up to be analogous to |char_width|.
  936. The |status| array tells if a charater has been downloaded to the
  937. laser printer, or if it is too large, and must be transferred as raster
  938. graphics. It might also contain a positive value that indicates a shift
  939. up of the reference point (because the ``baseline'' of the characters
  940. is set at |baseline| pixels from the bottom, characters can't be deeper than
  941. |baseline| pixels).
  942. The large arrays are located in
  943. EMA (Extended Memory Area) (outside the 32 page range addressable
  944. with one-word addresses).
  945. The \.{Ema\_Var} compiler options specify which variables are in EMA.
  946. @^system dependencies@>
  947. @d char_end_width(#)==#]
  948. @d char_pixel_width(#)==pixel_width[width_base[#]+char_end_width
  949. @d char_status(#)==status[width_base[#]+char_end_width
  950. @d not_loaded=0
  951. @d too_large=-1
  952. @d loaded_ok=-2
  953. @d baseline=55
  954. @<Glob...@>=
  955. @=$Ema_Var On$@>
  956. @!pixel_width:array[0..max_widths] of short; {actual character widths,
  957.   in pixels}
  958. @!status:array[0..max_widths] of short; {character statuses}
  959. @=$Ema_Var Off$@>
  960. @!conv:real; {converts \.{DVI} units to pixels}
  961. @!true_conv:real; {converts unmagnified \.{DVI} units to pixels}
  962. @!numerator,@!denominator:integer; {stated conversion ratio}
  963. @!mag:integer; {magnification factor times 1000}
  964. @!desired_mag:integer;
  965. @* Reading the font information.
  966. The current number of known fonts is |nf|. Each known font has
  967. an internal number |f|, where |0<=f<nf|; the external number of this font,
  968. i.e., its font identification number in the \.{DVI} file, is
  969. |font_num[f]|, and the external name of this font is the string that
  970. occupies positions |font_name[f]| through |font_name[f+1]-1| of the array
  971. |names|. The latter array consists of |ASCII_code| characters, and
  972. |font_name[nf]| is its first unoccupied position.  A horizontal motion
  973. in the range |-4*font_space[f]<h<font_space[f]|
  974. will be treated as a `kern'.
  975. A given character |c| is valid in font |f| if and only if
  976. |char_width(f)(c)<>invalid_width|.
  977. Finally, |char_width(f)(c)=width[width_base[f]+c]|, and |width_ptr| is the
  978. first unused position of the |width| array.
  979. @d char_width(#)==width[width_base[#]+char_end_width
  980. @d invalid_width==@'17777777777
  981. @<Glob...@>=
  982. @!font_num:array [0..max_fonts] of integer; {external font numbers}
  983. @!font_name:array [0..max_fonts] of 0..name_size; {starting positions
  984.   of external font names}
  985. @!names:array [0..name_size] of ASCII_code; {characters of names}
  986. @!font_check_sum:array [0..max_fonts] of integer; {check sums}
  987. @!font_scaled_size:array [0..max_fonts] of integer; {scale factors}
  988. @!font_design_size:array [0..max_fonts] of integer; {design sizes}
  989. @!font_mag:array [0..max_fonts] of integer;
  990. @!font_space:array [0..max_fonts] of integer; {boundary between ``small''
  991.   and ``large'' spaces}
  992. @!font_used_on:array[0..max_fonts] of integer; {on which page last used}
  993. @!width_base:array [0..max_fonts] of integer; {index into |width| table}
  994. @=$Ema_Var On$@>
  995. @!width:array [0..max_widths] of integer; {character widths, in \.{DVI} units}
  996. @=$Ema_Var Off$@>
  997. @!nf:0..max_fonts; {the number of known fonts}
  998. @!width_ptr:0..max_widths; {the number of known character widths}
  999. @ @<Set init...@>=
  1000. nf:=0; width_ptr:=0; font_name[0]:=0; font_space[0]:=0;
  1001. font_used_on[0]:=0; 
  1002. @ It is, of course, a simple matter to print the name of a given font.
  1003. @p procedure print_font(@!f:integer); {|f| is an internal font number}
  1004. var k:0..name_size; {index into |names|}
  1005. begin if f=nf then print('UNDEFINED!')
  1006. @.UNDEFINED@>
  1007. else  begin for k:=font_name[f] to font_name[f+1]-1 do
  1008.     print(xchr[names[k]]);
  1009.   end;
  1010. @ The global variabls |pxl_check_sum|,
  1011. |pxl_design_size| are set from the \.{PXL} file.
  1012. @<Glob...@>=
  1013. @!pxl_check_sum:integer; {check sum found in |pxl_file|}
  1014. @!pxl_dptr:array [0..max_fonts] of integer; {directory pointers}
  1015. @!pxl_design_size:integer;
  1016. @ Here is a procedure that absorbs the necessary information from a
  1017. \.{PXL} file, assuming that the file has just been successfully opened.
  1018. (A complete description of
  1019. \.{PXL} file format appears elsewhere and will
  1020. not be repeated here.) The procedure does not check the \.{PXL} file
  1021. for validity, nor does it give explicit information about what is
  1022. wrong with a \.{PXL} file that proves to be invalid; \.{DVI}-reading
  1023. programs need not do this, since \.{PXL} files are almost always valid.
  1024. The procedure simply returns |false| if it
  1025. detects anything amiss in the \.{PXL} data.
  1026. There is a parameter, |z|, which represents the scaling factor being
  1027. used to compute the font dimensions; it must be in the range $0<z<2^{27}$.
  1028. @d read_pxl(#)==begin #:=pxl_file[nf]^.i; get(pxl_file[nf]); end
  1029. @p procedure in_PXL(@!z:integer); {input \.{PXL} data}
  1030. var i,j,k,n:short; {indices etc.}
  1031. @!wp:0..max_widths; {new value of |width_ptr| after successful input}
  1032. @!raster_address:integer;
  1033. @!alpha,@!beta:integer; {quantities used in the scaling computation}
  1034. begin
  1035.   if width_ptr+128>max_widths then begin
  1036.     print_nl;
  1037.     abort('Need larger width table');
  1038.   end;
  1039.   width_base[nf]:=width_ptr;
  1040.   wp:=width_ptr;
  1041.   @<Check the header ID@>;
  1042.   @<Read the trailer@>;
  1043.   @<Read the font directory@>;
  1044.   width_ptr:=wp;
  1045. @ @<Check the header ID@>=
  1046. begin
  1047.   seek_pxl(nf)(0);
  1048.   get(pxl_file[nf]);
  1049.   if pxl_file[nf]^.i <> pxl_id then
  1050.     bad_pxl('bad header id');
  1051. @ @<Read the trailer@>=
  1052. begin
  1053.   seek(pxl_file[nf],s_pxl_file+1);
  1054.   get(pxl_file[nf]);
  1055.   if pxl_file[nf]^.i <> pxl_id then
  1056.     bad_pxl('bad trailer id');
  1057.   seek(pxl_file[nf],s_pxl_file-3);
  1058.   get(pxl_file[nf]);
  1059.   read_pxl(pxl_check_sum);
  1060.   read_pxl(font_mag[nf]);
  1061.   read_pxl(pxl_design_size);
  1062.   read_pxl(pxl_dptr[nf]);
  1063. @ One important part of |in_PXL| is the width computation, which
  1064. involves multiplying the relative widths in the \.{PXL} file by the
  1065. scaling factor in the \.{DVI} file. This fixed-point multiplication
  1066. must be done with precisely the same accuracy by all \.{DVI}-reading programs,
  1067. in order to validate the assumptions made by \.{DVI}-writing programs
  1068. like \TeX82.
  1069. Let us therefore summarize what needs to be done. Each width in a \.{PXL}
  1070. file appears as a four-byte quantity called a |fix_word|.  A |fix_word|
  1071. whose respective bytes are $(a,b,c,d)$ represents the number
  1072. $$x=\left\{\vcenter{\halign{$#$,\hfil\qquad&if $#$\hfil\cr
  1073. b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=0;\cr
  1074. -16+b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=255.\cr}}\right.$$
  1075. (No other choices of $a$ are allowed, since the magnitude of a \.{TFM}
  1076. dimension must be less than 16.)  We want to multiply this quantity by the
  1077. integer~|z|, which is known to be less than $2^{27}$. Let $\alpha=16z$.
  1078. If $|z|<2^{23}$, the individual multiplications $b\cdot z$, $c\cdot z$,
  1079. $d\cdot z$ cannot overflow; otherwise we will divide |z| by 2, 4, 8, or
  1080. 16, to obtain a multiplier less than $2^{23}$, and we can compensate for
  1081. this later. If |z| has thereby been replaced by $|z|^\prime=|z|/2^e$, let
  1082. $\beta=2^{4-e}$; we shall compute
  1083. $$\lfloor(b+c\cdot2^{-8}+d\cdot2^{-16})\,z^\prime/\beta\rfloor$$ if $a=0$,
  1084. or the same quantity minus $\alpha$ if $a=255$.  This calculation must be
  1085. done exactly, for the reasons stated above; the following program does the
  1086. job in a system-independent way, assuming that arithmetic is exact on
  1087. numbers less than $2^{31}$ in magnitude.
  1088. The following code computes pixel widths by simply rounding the \.{PXL}
  1089. widths to the nearest integer number of pixels, based on the conversion factor
  1090. |conv| that converts \.{DVI} units to pixels. However, such a simple
  1091. formula will not be valid for all fonts, and it will often give results that
  1092. are off by $\pm1$ when a low-resolution font has been carefully
  1093. hand-fitted. For example, a font designer often wants to make the letter `m'
  1094. a pixel wider or narrower in order to make the font appear more consistent.
  1095. \.{DVI}-to-printer programs should therefore input the correct pixel width
  1096. information from font files whenever there is a chance that it may differ.
  1097. A warning message may also be desirable in the case that at least one character
  1098. is found whose pixel width differs from |conv*width| by more than a full pixel.
  1099. Those characters that are too large to be downloadable, are marked as such,
  1100. and will be transferred using raster graphics. We must be especially careful
  1101. in the y dimension, as the character must fit into the 255*255 box when
  1102. the reference point is placed on the baseline. The baseline was set to
  1103. $255-$|baseline| pixel rows down from the top.
  1104. @^system dependencies@>
  1105. @d pixel_round(#)==round(conv*(#))
  1106. @<Read the font dir...@>=
  1107. @<Replace |z| by $|z|^\prime$ and compute $\alpha,\beta$@>;
  1108. for k:=0 to 127 do begin
  1109.   seek(pxl_file[nf],pxl_dptr[nf]+k*4+2);
  1110.   get(pxl_file[nf]);
  1111.   {is the charater too large ?}
  1112.   if (pxl_file[nf]^.i0 >= 128) or (pxl_file[nf]^.i1 >= 128) then
  1113.     status[wp]:=too_large
  1114.   else
  1115.     status[wp]:=not_loaded;
  1116.   get(pxl_file[nf]);
  1117.   get(pxl_file[nf]);
  1118.   if pxl_file[nf]^.i=0 then begin
  1119.     width[wp]:=invalid_width;
  1120.     pixel_width[wp]:=0  end
  1121.   else begin
  1122.     get(pxl_file[nf]);
  1123.     read_pxl_word(nf);
  1124.     width[wp]:=(((((b3*z)div@'400)+(b2*z))div@'400)+(b1*z))div beta;
  1125.     if b0>0 then if b0<255 then bad_pxl('strange width for char ', k:1)
  1126.       else width[wp]:=width[wp]-alpha;
  1127.     pixel_width[wp]:=pixel_round(width[wp]);
  1128.   end;
  1129.   incr(wp);
  1130. @ @<Replace |z|...@>=
  1131. begin alpha:=16*z; beta:=16;
  1132. while z>=@'40000000 do
  1133.   begin z:=z div 2; beta:=beta div 2;
  1134.   end;
  1135. @* Downloading information to the printer.
  1136. The procedure |update_pos| is used to update the printer cursor position.
  1137. This is the only place where the cursor is explicitely positioned.
  1138. @p procedure update_pos;
  1139. label done;
  1140. begin if (lj_h<>hh)or(lj_v<>vv) then begin
  1141.   write_lj(@=#27'*p'@>);
  1142.   if (lj_h<>hh) then begin
  1143.     if abs(lj_h-hh) > hh div 10 then
  1144.       write_lj_f(hh:1)
  1145.     else if (lj_h<hh) then
  1146.       write_lj_f('+', hh-lj_h:1)
  1147.     else
  1148.       write_lj_f('-', lj_h-hh:1);
  1149.     if (lj_v<>vv) then
  1150.       write_lj_f('x')
  1151.     else begin
  1152.       write_lj_f('X');
  1153.       goto done;
  1154.     end
  1155.   end;
  1156.   if abs(lj_v-vv) > vv div 10 then
  1157.     write_lj_f(vv:1)
  1158.   else if (lj_v<vv) then
  1159.     write_lj_f('+', vv-lj_v:1)
  1160.   else
  1161.     write_lj_f('-', lj_v-vv:1);
  1162.   write_lj_f('Y');
  1163. done:
  1164. lj_h:=hh; lj_v:=vv;
  1165. @ This procedure downloads a single character from a \.{PXL} file.
  1166. @^system dependencies@>
  1167. @p procedure download_char(p:integer);
  1168. var i,j:short;
  1169. @!pixel_wd,@!pixel_ht:integer;
  1170. @!offsets,char_wd,char_ht,delta_x:i2c;
  1171. @!raster_address:integer;
  1172. begin
  1173.   seek(pxl_file[cur_font],pxl_dptr[cur_font]+p*4+2);
  1174.   get(pxl_file[cur_font]);
  1175.   pixel_wd:=pxl_file[cur_font]^.i0;
  1176.   pixel_ht:=pxl_file[cur_font]^.i1;
  1177.   get(pxl_file[cur_font]);
  1178.   offsets:=pxl_file[cur_font]^;
  1179.   get(pxl_file[cur_font]);
  1180.   raster_address:=pxl_file[cur_font]^.i;
  1181.   @<Download character header@>;
  1182.   seek(pxl_file[cur_font],raster_address+2);
  1183.   for j:=1 to pixel_ht do begin
  1184.     for i:=1 to (pixel_wd-1) div 8 + 1 do begin
  1185.       if (i-1) mod 4 = 0 then
  1186.         get(pxl_file[cur_font]);
  1187.       write_lj(pxl_file[cur_font]^.c[(i-1) mod 4]);
  1188.     end;
  1189.   end
  1190. @ Download a character header.
  1191. @d data_bytes==(pixel_ht*(((pixel_wd-1) div 8)+1))
  1192. @<Download character header@>=
  1193. begin
  1194.   if pixel_ht - offsets.i1 > baseline then begin
  1195.     char_status(cur_font)(p):=pixel_ht-offsets.i1;
  1196.     offsets.i1:=pixel_ht;  end
  1197.   else
  1198.     char_status(cur_font)(p):=loaded_ok;
  1199.   offsets.i0:=-offsets.i0;
  1200.   char_wd.i0:=pixel_wd;
  1201.   char_ht.i0:=pixel_ht;
  1202.   delta_x.i0:=char_pixel_width(cur_font)(p)*4;
  1203.   write_lj(@=#27'*c'@>,font_num[cur_font]:1,'d',
  1204.     ord(vis_chr(p)):1,@='E'#27'(s'@>,
  1205.     data_bytes+16:1,'W'+
  1206.     @=#4#0#14#1#0#0@>,
  1207.     offsets.c[0], offsets.c[1],
  1208.     offsets.c[2], offsets.c[3],
  1209.     char_wd.c[0], char_wd.c[1],
  1210.     char_ht.c[0], char_ht.c[1],
  1211.     delta_x.c[0], delta_x.c[1]);
  1212. @ This procedure transfers a character in raster form.
  1213. This is used for characters which are too large to be stored in
  1214. the font memory of the printer.
  1215. @^system dependencies@>
  1216. @p procedure raster_char(p:integer);
  1217. i,j,n:short;
  1218. @!pixel_ht,@!pixel_wd,@!x_offset,@!y_offset:short;
  1219. @!raster_address:integer;
  1220. begin
  1221.   seek(pxl_file[cur_font],pxl_dptr[cur_font]+p*4+2);
  1222.   get(pxl_file[cur_font]);
  1223.   pixel_wd:=pxl_file[cur_font]^.i0;
  1224.   pixel_ht:=pxl_file[cur_font]^.i1;
  1225.   get(pxl_file[cur_font]);
  1226.   x_offset:=pxl_file[cur_font]^.i0;
  1227.   y_offset:=pxl_file[cur_font]^.i1;
  1228.   get(pxl_file[cur_font]);
  1229.   raster_address:=pxl_file[cur_font]^.i;
  1230.   hh:=hh-x_offset; vv:=vv-y_offset;
  1231.   update_pos;
  1232.   write_lj(@=#27'*r1A'@>);
  1233.   seek(pxl_file[cur_font],raster_address+2);
  1234.   for j:=1 to pixel_ht do begin
  1235.     write_lj(#27'*b',(pixel_wd-1) div 8 + 1:1, 'W');
  1236.     for i:=1 to (pixel_wd-1) div 8 + 1 do begin
  1237.       if (i-1) mod 4 = 0 then
  1238.         get(pxl_file[cur_font]);
  1239.       write_lj(pxl_file[cur_font]^.c[(i-1) mod 4]);
  1240.     end;
  1241.   end;
  1242.   lj_v:=lj_v+pixel_ht;
  1243.   hh:=hh+char_pixel_width(cur_font)(p); vv:=vv+y_offset;
  1244.   write_lj(@=#27'*rB'@>);
  1245. @* Optional modes of output.
  1246. The starting page is specified by giving a sequence of 1 to 10 numbers or
  1247. asterisks separated by dots. For example, the specification `\.{1.*.-5}'
  1248. can be used to refer to a page output by \TeX\ when $\.{\\count0}=1$
  1249. and $\.{\\count2}=-5$. (Recall that |bop| commands in a \.{DVI} file
  1250. are followed by ten `count' values.) An asterisk matches any number,
  1251. so the `\.*' in `\.{1.*.-5}' means that \.{\\count1} is ignored when
  1252. specifying the first page. If several pages match the given specification,
  1253. \.{DVIplus} will begin with the earliest such page in the file. The
  1254. default specification `\.*' (which matches all pages) therefore denotes
  1255. the page at the beginning of the file.
  1256. Another option is the page offset. That is the point on the physical
  1257. printer page where the point (0,0) in the DVI file is mapped.
  1258. Normally \.{DVIplus} uses default values for the options.
  1259. It can be started in such a way that it engages
  1260. the user in a brief dialog so that the
  1261. options will be specified.
  1262. @^system dependencies@>
  1263. @<Glob...@>=
  1264. @!max_pages:integer; {at most this many |bop..eop| pages will be printed}
  1265. @!resolution:real; {pixels per inch}
  1266. @!new_mag:integer; {if positive, overrides the postamble's magnification}
  1267. @!copies:integer;
  1268. @!h_offset,@!v_offset:integer; {offset where to put (0,0) on physical page}
  1269. @ The starting page specification is recorded in two global arrays called
  1270. |start_count| and |start_there|. For example, `\.{1.*.-5}' is represented
  1271. by |start_there[0]=true|, |start_count[0]=1|, |start_there[1]=false|,
  1272. |start_there[2]=true|, |start_count[2]=-5|.
  1273. We also set |start_vals=2|, to indicate that count 2 was the last one
  1274. mentioned. The other values of |start_count| and |start_there| are not
  1275. important, in this example.
  1276. @<Glob...@>=
  1277. @!start_count:array[0..9] of integer; {count values to select starting page}
  1278. @!start_there:array[0..9] of boolean; {is the |start_count| value relevant?}
  1279. @!start_vals:0..9; {the last count considered significant}
  1280. @!count:array[0..9] of integer; {the count values on the current page}
  1281. @ @<Set init...@>=
  1282. max_pages:=100; start_vals:=0; start_there[0]:=false; copies:=1;
  1283. resolution:=300.0; new_mag:=0;
  1284. h_offset:=210; v_offset:=100;
  1285. @ Here is a simple subroutine that tests if the current page might be the
  1286. starting page.
  1287. @p function start_match:boolean; {does |count| match the starting spec?}
  1288. var k:0..9;  {loop index}
  1289. @!match:boolean; {does everything match so far?}
  1290. begin match:=true;
  1291. for k:=0 to start_vals do
  1292.   if start_there[k]and(start_count[k]<>count[k]) then match:=false;
  1293. start_match:=match;
  1294. @ The |input_ln| routine waits for the user to type a line at his or her
  1295. terminal; then it puts ASCII-code equivalents for the characters on that line
  1296. into the |buffer| array. The |term_in| file is used for terminal input,
  1297. and |term_out| for terminal output.
  1298. @^system dependencies@>
  1299. @<Glob...@>=
  1300. @!buffer:array[0..terminal_line_length] of ASCII_code;
  1301. @!term_in:text_file; {the terminal, considered as an input file}
  1302. @!term_out:text_file; {the terminal, considered as an output file}
  1303. @!arg_index:short; {which command line argument is being processed}
  1304. @!interactive:boolean;
  1305. @ Since the terminal is being used for both input and output, some systems
  1306. need a special routine to make sure that the user can see a prompt message
  1307. before waiting for input based on that message. (Otherwise the message
  1308. may just be sitting in a hidden buffer somewhere, and the user will have
  1309. no idea what the program is waiting for.) We shall call a system-dependent
  1310. subroutine |update_terminal| in order to avoid this problem.
  1311. @^system dependencies@>
  1312. @d update_terminal == prompt(term_out) {empty the terminal output buffer}
  1313. @ During the dialog, \.{DVIplus} will treat the first blank space in a
  1314. line as the end of that line. Therefore |input_ln| makes sure that there
  1315. is always at least one blank space in |buffer|.
  1316. @^system dependencies@>
  1317. @p procedure input_ln; {inputs a line from the terminal}
  1318. var k:0..terminal_line_length;
  1319. begin update_terminal;
  1320. k:=0;
  1321. if eoln(term_in) then read_ln(term_in)
  1322. else begin while (k<terminal_line_length)and not eoln(term_in) do
  1323.   begin buffer[k]:=xord[term_in^]; incr(k); get(term_in);
  1324.   end;
  1325.   read_ln(term_in);
  1326. buffer[k]:=" ";
  1327. @ The global variable |buf_ptr| is used while scanning each line of input;
  1328. it points to the first unread character in |buffer|.
  1329. @<Glob...@>=
  1330. @!buf_ptr:0..terminal_line_length; {the number of characters read}
  1331. @ Here is a routine that scans a (possibly signed) integer and computes
  1332. the decimal value. If no decimal integer starts at |buf_ptr|, the
  1333. value 0 is returned. The integer should be less than $2^{31}$ in
  1334. absolute value.
  1335. @p function get_integer:integer;
  1336. var x:integer; {accumulates the value}
  1337. @!negative:boolean; {should the value be negated?}
  1338. begin if buffer[buf_ptr]="-" then
  1339.   begin negative:=true; incr(buf_ptr);
  1340.   end
  1341. else negative:=false;
  1342. x:=0;
  1343. while (buffer[buf_ptr]>="0")and(buffer[buf_ptr]<="9") do
  1344.   begin x:=10*x+buffer[buf_ptr]-"0"; incr(buf_ptr);
  1345.   end;
  1346. if negative then get_integer:=-x @+ else get_integer:=x;
  1347. @ The selected options are put into global variables by the |dialog|
  1348. procedure, which is called just as \.{DVIplus} begins.
  1349. @^system dependencies@>
  1350. @p procedure dialog;
  1351. label 2,3,6,7,8,9,99;
  1352. var i,j,k:short;
  1353. begin rewrite(term_out, '1', 'NOCCTL'); {prepare the terminal for output}
  1354. reset(term_in, '1'); {and for input}
  1355. print_ln(banner);
  1356. @<Get flags, check if interactive@>;
  1357. @<Determine the desired |start_count| values@>;
  1358. @<Determine the desired |max_pages|@>;
  1359. @<Determine the number of copies@>;
  1360. @<Determine the page offset@>;
  1361. @<Print all the selected options@>;
  1362. @ @<Get flags, check if interactive@>=
  1363. arg_index:=1; interactive:=false;
  1364. repeat
  1365.   i:=parameters(arg_index,cur_name,name_length);
  1366.   if cur_name[1]='-' then begin
  1367.     incr(arg_index);
  1368.     if i=1 then
  1369.       interactive:=true
  1370.     else case cur_name[2] of
  1371.       'i','I':interactive:=true;
  1372.     othercases begin end;
  1373.     endcases;
  1374.   end;
  1375. until cur_name[1]<>'-';
  1376. if not interactive then goto 99;
  1377. @ @<Determine the desired |start...@>=
  1378. 2: print('Starting page (default=*): ');
  1379. input_ln; buf_ptr:=0; k:=0;
  1380. if buffer[0]<>" " then
  1381.   repeat if buffer[buf_ptr]="*" then
  1382.     begin start_there[k]:=false; incr(buf_ptr);
  1383.     end
  1384.   else  begin start_there[k]:=true; start_count[k]:=get_integer;
  1385.     end;
  1386.   if (k<9)and(buffer[buf_ptr]=".") then
  1387.     begin incr(k); incr(buf_ptr);
  1388.     end
  1389.   else if buffer[buf_ptr]=" " then start_vals:=k
  1390.   else  begin print('Type, e.g., 1.*.-5 to specify the ');
  1391.     print_ln('first page with \count0=1, \count2=-5.');
  1392.     goto 2;
  1393.     end;
  1394.   until start_vals=k
  1395. @ @<Determine the desired |max_pages|@>=
  1396. 3: print('Maximum number of pages (default=100): ');
  1397. input_ln; buf_ptr:=0;
  1398. if buffer[0]<>" " then
  1399.   begin max_pages:=get_integer;
  1400.   if max_pages<=0 then
  1401.     begin print_ln('Please type a positive number.');
  1402.     goto 3;
  1403.     end;
  1404.   end
  1405. @ @<Determine the number of copies@>=
  1406. 6: print('Number of copies (default=1): ');
  1407. input_ln; buf_ptr:=0;
  1408. if buffer[0]<>" " then
  1409.   if (buffer[0]>="0")and(buffer[0]<="9") then copies:=get_integer
  1410.   else  begin print('Type a positive integer to specify ');
  1411.     print_ln('the number of copies.');
  1412.     goto 6;
  1413.     end
  1414. @ @<Determine the page offset@>=
  1415. 7: print('Page offset in dots (default=210,100): ');
  1416. input_ln; buf_ptr:=0;
  1417. if buffer[0]=" " then goto 9;
  1418. if ((buffer[0]>="0")and(buffer[0]<="9")) or (buffer[0]="-") then
  1419.  h_offset:=get_integer
  1420. else goto 8;
  1421. if (buffer[buf_ptr]=",") then incr(buf_ptr);
  1422. if (buffer[buf_ptr]=" ") then incr(buf_ptr);
  1423. if ((buffer[buf_ptr]>="0")and(buffer[buf_ptr]<="9")) or
  1424.    (buffer[buf_ptr]="-") then begin
  1425.   v_offset:=get_integer;
  1426.   goto 9; end
  1427. else goto 8;
  1428. 8:print('Specify a dot coordinate pair where the (0,0) ');
  1429.   print_ln('point will be placed.');
  1430.   goto 7;
  1431. @ After the dialog is over, we print the options so that the user
  1432. can see what \.{DVIplus} thought was specified.
  1433. @<Print all the selected options@>=
  1434. 99:print_ln('Options selected:');
  1435. @.Options selected@>
  1436. print('  Starting page = ');
  1437. for k:=0 to start_vals do
  1438.   begin if start_there[k] then print(start_count[k]:1)
  1439.   else print('*');
  1440.   if k<start_vals then print('.')
  1441.   else print_nl;
  1442.   end;
  1443. print_ln('  Maximum number of pages = ',max_pages:1);
  1444. print_ln('  Page offset = (', h_offset:1, ',', v_offset:1, ')');
  1445. @* Defining fonts.
  1446. \.{DVIplus} reads the postamble first and loads
  1447. all of the fonts defined there; then it processes the pages.
  1448. @ Approximate the desired magnification to an available one.
  1449. The ``magig numbers'' that the desired magnification
  1450. is compared to are calculated as
  1451. $1500*1.2^m$, where $m = 1/4,3/4,1.5,2.5,3.5,4.5$
  1452. @p function approx_mag(f:integer;d_mag:integer):integer;
  1453. begin
  1454.   if d_mag < 1569 then
  1455.     approx_mag := 1500
  1456.   else if d_mag < 1720 then
  1457.     approx_mag := 1643
  1458.   else if d_mag < 1971 then
  1459.     approx_mag := 1800
  1460.   else if d_mag < 2366 then
  1461.     approx_mag := 2160
  1462.   else if d_mag < 2839 then
  1463.     approx_mag := 2592
  1464.   else if d_mag < 3407 then
  1465.     approx_mag := 3110
  1466.   else if d_mag < 4089 then
  1467.     approx_mag := 3732
  1468.   else
  1469.     approx_mag:= 4479;
  1470. @ The following subroutine does the necessary things when a \\{fnt\_def}
  1471. command is being processed.
  1472. @p procedure define_font(@!e:integer); {|e| is an external font number}
  1473. var f:0..max_fonts;
  1474. @!p:integer; {length of the area/directory spec}
  1475. @!n:integer; {length of the font name proper}
  1476. @!c,@!q,@!d:integer; {check sum, scaled size, and design size}
  1477. @!m:integer; {|mag| corrected for 300 pixels/inch}
  1478. @!r:0..name_length; {index into |cur_name|}
  1479. @!j,@!k:0..name_size; {indices into |names|}
  1480. @!mismatch:boolean; {do names disagree?}
  1481. begin if nf=max_fonts then abort('DVIplus capacity exceeded (max fonts=',
  1482.     max_fonts:1,')!');
  1483. @.DVIplus capacity exceeded...@>
  1484. font_num[nf]:=e; f:=0;
  1485. while font_num[f]<>e do incr(f);
  1486. @<Read the font parameters into position for font |nf|, and
  1487.   print the font name@>;
  1488.   if f<nf then print_ln('---this font was already defined!');
  1489. @.this font was already defined@>
  1490.   @<Load the new font, unless there are problems@>
  1491. @ @<Read the font parameters into position for font |nf|...@>=
  1492. c:=signed_quad; font_check_sum[nf]:=c;@/
  1493. q:=signed_quad; font_scaled_size[nf]:=q;@/
  1494. d:=signed_quad; font_design_size[nf]:=d;@/
  1495. p:=get_byte; n:=get_byte;
  1496. if font_name[nf]+n+p>name_size then
  1497.   abort('DVIplus capacity exceeded (name size=',name_size:1,')');
  1498. @.DVIplus capacity exceeded...@>
  1499. font_name[nf+1]:=font_name[nf]+n+p;
  1500. if n+p=0 then abort('Null font name')
  1501. @.Null font name@>
  1502. else for k:=font_name[nf] to font_name[nf+1]-1 do names[k]:=get_byte;
  1503. font_used_on[nf+1]:=0;
  1504. incr(nf);
  1505. if f=nf-1 then begin
  1506.   print('Font ', e:1, ': '); print_font(nf-1);
  1507.   update_terminal;
  1508. decr(nf)
  1509. @ @<Load the new font, unless there are problems@>=
  1510. begin
  1511. @<Compute |desired_mag|@>;
  1512. @<Move font name into the |cur_name| string@>;
  1513. if not open_pxl_file(nf) then begin
  1514.   print_nl;
  1515.   abort('Cannot open PXL file ', cur_name); end
  1516. @.Cannot open PXL file@>
  1517. else  begin if (q<=0)or(q>=@'1000000000) then begin
  1518.     print_nl;
  1519.     abort('PXL file not loaded, bad scale (',q:1,')!'); end
  1520. @.bad scale@>
  1521.   else if (d<=0)or(d>=@'1000000000) then begin
  1522.     print_nl;
  1523.     abort('PXL file not loaded, bad design size (',d:1,')!'); end
  1524. @.bad design size@>
  1525.   else begin
  1526.     in_PXL(q);
  1527.     @<Finish loading the new font info@>;
  1528.   end
  1529. @ @<Compute |desired_mag|@>=
  1530. desired_mag:=round((mag * q * (300.0/200.0))/d+0.5);
  1531. @ @<Finish loading...@>=
  1532. begin font_space[nf]:=q div 6; {this is a 3-unit ``thin space''}
  1533. if (c<>0)and(pxl_check_sum<>0)and(c<>pxl_check_sum) then
  1534.   begin print(' ---beware: check sums do not agree!');
  1535. @.beware: check sums do not agree@>
  1536. @.check sums do not agree@>
  1537.   print(' (',c:1,' vs. ',pxl_check_sum:1,')');
  1538.   end;
  1539. d:=round((100.0*conv*q)/(true_conv*d));
  1540. if d<>100 then
  1541.   print(' (magnified ',d:1,'%)');
  1542. @.this font is magnified@>
  1543. incr(nf); {now the new font is officially present}
  1544. font_space[nf]:=0; {for |out_space| and |out_vmove|}
  1545. @ If |p=0|, i.e., if no font directory has been specified, \.{DVIplus}
  1546. uses the default font directory, which is a
  1547. system-dependent place where the standard fonts are kept.
  1548. In RTE--A, the \.{PXL} files are kept in directories called
  1549. \.{/TeX/Fonts/MagXXXX}, where \.{XXXX} is the magnification.
  1550. The string variable |default_prefix| contains the prefix of these names.
  1551. @^system dependencies@>
  1552. @d default_prefix_name=='/TeX/Fonts/Mag'
  1553. @d default_prefix_length=14 {change this to the correct length}
  1554. @<Glob...@>=
  1555. @!default_prefix:packed array[1..default_prefix_length] of char;
  1556. @ @<Set init...@>=
  1557. default_prefix:=default_prefix_name;
  1558. @ The string |cur_name| is set to the external name of the
  1559. \.{PXL} file for the current font and magnification.
  1560. @^system dependencies@>
  1561. @<Move font name into the |cur_name| string@>=
  1562. for k:=1 to name_length do cur_name[k]:=' ';
  1563. if p=0 then
  1564.   begin for k:=1 to default_prefix_length do
  1565.     cur_name[k]:=default_prefix[k];
  1566.   r:=default_prefix_length;
  1567.   incr(r);
  1568.   best_mag:=approx_mag(font_name[nf],desired_mag);
  1569.   m:=best_mag;
  1570.   cur_name[r]:=xchr[ m div 1000 + xord['0']];
  1571.   incr(r);
  1572.   cur_name[r]:=xchr[ (m div 100) mod 10 + xord['0']];
  1573.   incr(r);
  1574.   cur_name[r]:=xchr[ (m div 10) mod 10 + xord['0']];
  1575.   incr(r);
  1576.   cur_name[r]:=xchr[ m mod 10 + xord['0']];
  1577.   incr(r);
  1578.   cur_name[r]:='/'; end
  1579.   r:=0;
  1580. for k:=font_name[nf] to font_name[nf+1]-1 do
  1581.   begin incr(r);
  1582.   if r+4>name_length then
  1583.     abort('DVIplus capacity exceeded (max font name length=',
  1584.       name_length:1,')!');
  1585. @.DVIplus capacity exceeded...@>
  1586.   cur_name[r]:=xchr[names[k]];
  1587.   end;
  1588. cur_name[r+1]:='.'; cur_name[r+2]:='P'; cur_name[r+3]:='X'; cur_name[r+4]:='L'
  1589. @* Interpreting the {\tentex DVI} file.
  1590. The main work of \.{DVIplus} is accomplished by the |do_page| procedure,
  1591. which produces the output for an entire page, assuming that the |bop|
  1592. command for that page has already been processed. This procedure is
  1593. essentially an interpretive routine that reads and acts on the \.{DVI}
  1594. commands.
  1595. @ The definition of \.{DVI} files refers to six registers,
  1596. $(h,v,w,x,y,z)$, which hold integer values in \.{DVI} units.  In practice,
  1597. we also need registers |hh| and |vv|, the pixel analogs of $h$ and $v$,
  1598. since it is not always true that |hh=pixel_round(h)| or
  1599. |vv=pixel_round(v)|.
  1600. The |lj_h| and |lj_v| registers hold the current actual cursor position
  1601. of the printer.
  1602. The stack of $(h,v,w,x,y,z)$ values is represented by eight arrays
  1603. called |hstack|, \dots, |zstack|, |hhstack|, and |vvstack|.
  1604. @<Glob...@>=
  1605. @!h,@!v,@!w,@!x,@!y,@!z,@!hh,@!vv:integer; {current state values}
  1606. @!lj_h,@!lj_v:integer; {current cursor position}
  1607. @!hstack,@!vstack,@!wstack,@!xstack,@!ystack,@!zstack:
  1608.   array [0..stack_size] of integer; {pushed down values in \.{DVI} units}
  1609. @!hhstack,@!vvstack:
  1610.   array [0..stack_size] of integer; {pushed down values in pixels}
  1611. @ Three characteristics of the pages (their |max_v|, |max_h|, and
  1612. |max_s|) are specified in the postamble, and a warning message
  1613. is printed if these limits are exceeded. Actually |max_v| is set to
  1614. the maximum height plus depth of a page, and |max_h| to the maximum width,
  1615. for purposes of page layout. Since characters can legally be set outside
  1616. of the page boundaries, it is not an error when |max_v| or |max_h| is
  1617. exceeded. But |max_s| should not be exceeded.
  1618. The postamble also specifies the total number of pages; \.{DVIplus}
  1619. checks to see if this total is accurate.
  1620. @<Glob...@>=
  1621. @!max_v:integer; {the value of |abs(v)| should probably not exceed this}
  1622. @!max_h:integer; {the value of |abs(h)| should probably not exceed this}
  1623. @!max_s:integer; {the stack depth should not exceed this}
  1624. @!max_v_so_far,@!max_h_so_far,@!max_s_so_far:integer; {the record high levels}
  1625. @!total_pages:integer; {the stated total number of pages}
  1626. @!page_count:integer; {the total number of pages seen so far}
  1627. @ @<Set init...@>=
  1628. max_v:=@'17777777777-99; max_h:=@'17777777777-99; max_s:=stack_size+1;@/
  1629. max_v_so_far:=0; max_h_so_far:=0; max_s_so_far:=0; page_count:=0;
  1630. @ Before we get into the details of |do_page|, it is convenient to
  1631. consider a simpler routine that computes the first parameter of each
  1632. opcode.
  1633. @d four_cases(#)==#,#+1,#+2,#+3
  1634. @d eight_cases(#)==four_cases(#),four_cases(#+4)
  1635. @d sixteen_cases(#)==eight_cases(#),eight_cases(#+8)
  1636. @d thirty_two_cases(#)==sixteen_cases(#),sixteen_cases(#+16)
  1637. @d sixty_four_cases(#)==thirty_two_cases(#),thirty_two_cases(#+32)
  1638. @p function first_par(o:eight_bits):integer;
  1639. begin case o of
  1640. sixty_four_cases(set_char_0),sixty_four_cases(set_char_0+64):
  1641.   first_par:=o-set_char_0;
  1642. set1,put1,fnt1,xxx1,fnt_def1: first_par:=get_byte;
  1643. set1+1,put1+1,fnt1+1,xxx1+1,fnt_def1+1: first_par:=get_two_bytes;
  1644. set1+2,put1+2,fnt1+2,xxx1+2,fnt_def1+2: first_par:=get_three_bytes;
  1645. right1,w1,x1,down1,y1,z1: first_par:=signed_byte;
  1646. right1+1,w1+1,x1+1,down1+1,y1+1,z1+1: first_par:=signed_pair;
  1647. right1+2,w1+2,x1+2,down1+2,y1+2,z1+2: first_par:=signed_trio;
  1648. set1+3,set_rule,put1+3,put_rule,right1+3,w1+3,x1+3,down1+3,y1+3,z1+3,
  1649.   fnt1+3,xxx1+3,fnt_def1+3: first_par:=signed_quad;
  1650. nop,bop,eop,push,pop,pre,post,post_post,undefined_commands: first_par:=0;
  1651. w0: first_par:=w;
  1652. x0: first_par:=x;
  1653. y0: first_par:=y;
  1654. z0: first_par:=z;
  1655. sixty_four_cases(fnt_num_0): first_par:=o-fnt_num_0;
  1656. @ Here is another subroutine that we need: It computes the number of
  1657. pixels in the height or width of a rule. Characters and rules will line up
  1658. properly if the sizes are computed precisely as specified here.  (Since
  1659. |conv| is computed with some floating-point roundoff error, in a
  1660. machine-dependent way, format designers who are tailoring something for a
  1661. particular resolution should not plan their measurements to come out to an
  1662. exact integer number of pixels; they should compute things so that the
  1663. rule dimensions are a little less than an integer number of pixels, e.g.,
  1664. 4.99 instead of 5.00.)
  1665. @p function rule_pixels(x:integer):integer;
  1666.   {computes $\lceil|conv|\cdot x\rceil$}
  1667. var n:integer;
  1668. begin n:=trunc(conv*x);
  1669. if n<conv*x then rule_pixels:=n+1 @+ else rule_pixels:=n;
  1670. @ Strictly speaking, the |do_page| procedure is really a function with
  1671. side effects, not a `\&{procedure}'; it returns the value |false| if
  1672. \.{DVIplus} should be aborted because of some unusual happening. The
  1673. subroutine is organized as a typical interpreter, with a multiway branch
  1674. on the command code followed by |goto| statements leading to routines that
  1675. finish up the activities common to different commands. We will use the
  1676. following labels:
  1677. @d fin_set=41 {label for commands that set or put a character}
  1678. @d fin_rule=42 {label for commands that set or put a rule}
  1679. @d move_right=43 {label for commands that change |h|}
  1680. @d move_down=44 {label for commands that change |v|}
  1681. @d change_font=46 {label for commands that change |cur_font|}
  1682. @ Some \PASCAL\ compilers severely restrict the length of procedure bodies,
  1683. so we shall split |do_page| into two parts, one of which is
  1684. called |special_cases|. The different parts communicate with each other
  1685. via the global variables mentioned above, together with the following ones:
  1686. @<Glob...@>=
  1687. @!s:integer; {current stack size}
  1688. @!ss:integer; {stack size to print}
  1689. @!cur_font:integer; {current internal font number}
  1690. @!prev_font:integer;
  1691. @!fonts_in_use:integer; {how many are currently loaded}
  1692. @!fonts_on_page:integer; {how many fonts used on current page}
  1693. @ @<Set init...@>=
  1694. fonts_in_use:=0;
  1695. @ Here is the overall setup.
  1696. @p @t\4@>@<Declare the function called |special_cases|@>@;
  1697. procedure do_page;
  1698. label fin_set,fin_rule,move_right,done,exit;
  1699. var o:eight_bits; {operation code of the current command}
  1700. @!p,@!q:integer; {parameters of the current command}
  1701. pp,qq:integer;
  1702. i,j,k:integer;
  1703. @!a:integer; {byte number of the current command}
  1704. @!hhh:integer; {|h|, rounded to the nearest pixel}
  1705. @!height,pitch:i2c;
  1706. begin cur_font:=nf; {set current font undefined}
  1707. prev_font:=-1;
  1708. s:=0; h:=round(h_offset/conv); v:=round(v_offset/conv);
  1709. w:=0; x:=0; y:=0; z:=0; hh:=pixel_round(h); vv:=pixel_round(v);
  1710. lj_h:=-10000; lj_v:=-10000;
  1711.   {initialize the state variables}
  1712. while true do @<Translate the next command in the \.{DVI} file@>;
  1713. exit:
  1714. @d error(#)==print_ln('% ',#)
  1715. @<Translate the next command...@>=
  1716. begin a:=cur_loc;
  1717. o:=get_byte; p:=first_par(o);
  1718. if eof_dvi_file then bad_dvi('file ended prematurely');
  1719. @.the file ended prematurely@>
  1720. @<Start translation of command |o| and |goto| the appropriate label to
  1721.   finish the job@>;
  1722. fin_set: @<Finish a command that either sets or puts a character, then
  1723.     |goto move_right| or |done|@>;
  1724. fin_rule: @<Finish a command that either sets or puts a rule, then
  1725.     |goto move_right| or |done|@>;
  1726. move_right: @<Finish a command that sets |h:=h+q|, then |goto done|@>;
  1727. done:
  1728. @ The multiway switch in |first_par|, above, was organized by the length
  1729. of each command; the one in |do_page| is organized by the semantics.
  1730. @<Start translation...@>=
  1731. if o<set_char_0+128 then @<Translate a |set_char| command@>
  1732. else case o of
  1733.   four_cases(set1),
  1734.   four_cases(put1): bad_dvi('illegal character (', p:1, ')');
  1735.   set_rule,
  1736.   put_rule: goto fin_rule;
  1737.   @t\4@>@<Cases for commands |nop|, |bop|, \dots, |pop|@>@;
  1738.   @t\4@>@<Cases for horizontal motion@>@;
  1739.   othercases if special_cases(o,p,a) then goto done
  1740.              else bad_dvi(' ')
  1741.   endcases
  1742. @ @<Declare the function called |special_cases|@>=
  1743. function special_cases(@!o:eight_bits;@!p,@!a:integer):boolean;
  1744. label change_font,move_down,done,9998;
  1745. var q:integer; {parameter of the current command}
  1746. q1,q2:integer; {dummies for |fnt_def|}
  1747. @!k:integer; {loop index}
  1748. @!bad_char:boolean; {has a non-ASCII character code appeared in this \\{xxx}?}
  1749. @!pure:boolean; {is the command error-free?}
  1750. @!vvv:integer; {|v|, rounded to the nearest pixel}
  1751. begin pure:=true;
  1752. case o of
  1753. @t\4@>@<Cases for vertical motion@>@;
  1754. @t\4@>@<Cases for fonts@>@;
  1755. four_cases(xxx1): @<Translate an |xxx| command and |goto done|@>;
  1756. pre: begin error('preamble command within a page!'); goto 9998;
  1757.   end;
  1758. @.preamble command within a page@>
  1759. post,post_post: begin error('postamble command within a page!'); goto 9998;
  1760. @.postamble command within a page@>
  1761.   end;
  1762. othercases begin error('undefined command ',o:1,'!');
  1763.   goto done;
  1764. @.undefined command@>
  1765.   end
  1766. endcases;
  1767. move_down: @<Finish a command that sets |v:=v+p|, then |goto done|@>;
  1768. change_font: @<Finish a command that changes the current font,
  1769.   then |goto done|@>;
  1770. 9998: pure:=false;
  1771. done: special_cases:=pure;
  1772. @ @<Cases for commands |nop|, |bop|, \dots, |pop|@>=
  1773. nop: goto done;
  1774. bop: begin error('bop occurred before eop'); bad_dvi(' ');
  1775. @.bop occurred before eop@>
  1776.   end;
  1777. eop: begin
  1778.   write_lj(#12);
  1779.   if s<>0 then error('stack not empty at end of page (level ',
  1780.     s:1,')!');
  1781. @.stack not empty...@>
  1782.   return;
  1783.   end;
  1784. push: begin
  1785.   if s=max_s_so_far then
  1786.     begin max_s_so_far:=s+1;
  1787.     if s=max_s then error('deeper than claimed in postamble!');
  1788. @.deeper than claimed...@>
  1789. @.push deeper than claimed...@>
  1790.     if s=stack_size then
  1791.       abort('DVIplus capacity exceeded (stack size=',
  1792.         stack_size:1,')');
  1793.     end;
  1794.   hstack[s]:=h; vstack[s]:=v; wstack[s]:=w;
  1795.   xstack[s]:=x; ystack[s]:=y; zstack[s]:=z;
  1796.   hhstack[s]:=hh; vvstack[s]:=vv; incr(s); ss:=s-1; goto done;
  1797.   end;
  1798. pop: begin
  1799.   if s=0 then error('(illegal at level zero)!')
  1800.   else  begin decr(s); hh:=hhstack[s]; vv:=vvstack[s];
  1801.     h:=hstack[s]; v:=vstack[s]; w:=wstack[s];
  1802.     x:=xstack[s]; y:=ystack[s]; z:=zstack[s];
  1803.     end;
  1804.   ss:=s; goto done;
  1805.   end;
  1806. @ Rounding to the nearest pixel is best done in the manner shown here, so as
  1807. to be inoffensive to the eye: When the horizontal motion is small, like a
  1808. kern, |hh| changes by rounding the kern; but when the motion is large, |hh|
  1809. changes by rounding the true position |h| so that accumulated rounding errors
  1810. disappear. We allow a larger space in the negative direction than in
  1811. the positive one, because \TeX\ makes comparatively
  1812. large backspaces when it positions accents.
  1813. @d out_space==begin
  1814.   if (p>=font_space[cur_font])or(p<=-4*font_space[cur_font]) then
  1815.     hh:=pixel_round(h+p)
  1816.   else hh:=hh+pixel_round(p);
  1817.   q:=p; goto move_right;
  1818. @<Cases for horizontal motion@>=
  1819. four_cases(right1):out_space;
  1820. w0,four_cases(w1):begin w:=p; out_space;
  1821.   end;
  1822. x0,four_cases(x1):begin x:=p; out_space;
  1823.   end;
  1824. @ Vertical motion is done similarly, but with the threshold between
  1825. ``small'' and ``large'' increased by a factor of five. The idea is to make
  1826. fractions like ``$1\over2$'' round consistently, but to absorb accumulated
  1827. rounding errors in the baseline-skip moves.
  1828. @d out_vmove==begin
  1829.   if abs(p)>=5*font_space[cur_font] then vv:=pixel_round(v+p)
  1830.   else vv:=vv+pixel_round(p);
  1831.   goto move_down;
  1832. @<Cases for vertical motion@>=
  1833. four_cases(down1):out_vmove;
  1834. y0,four_cases(y1):begin y:=p; out_vmove;
  1835.   end;
  1836. z0,four_cases(z1):begin z:=p; out_vmove;
  1837.   end;
  1838. @ @<Cases for fonts@>=
  1839. sixty_four_cases(fnt_num_0),
  1840. four_cases(fnt1):
  1841.   goto change_font;
  1842. four_cases(fnt_def1): begin
  1843.   @<Skip a |fnt_def| command@>;
  1844.   goto done;
  1845.   end;
  1846. @ @<Skip a |fnt_def| command@>=
  1847. q:=signed_quad; q:=signed_quad; q:=signed_quad;
  1848. q1:=get_byte; q2:=get_byte;
  1849. for k:=1 to q1+q2 do q:=get_byte;
  1850. @ @<Translate an |xxx| command and |goto done|@>=
  1851. begin bad_char:=false;
  1852. for k:=1 to p do
  1853.   begin q:=get_byte;
  1854.   if not ((q>=" ")and(q<="~")) then
  1855.     bad_char:=true;
  1856.   end;
  1857. if bad_char then error('non-ASCII character in xxx command!');
  1858. @.non-ASCII character...@>
  1859. goto done;
  1860. @ @<Translate a |set_char|...@>=
  1861. goto fin_set
  1862. @ This is the code that checks whether the next character to be
  1863. printed in |cur_font| has occurred previously.
  1864. If not, the character data is downloaded to the printer. If the character
  1865. is too large, the data is sent as a raster image.
  1866. If |cur_font| is also new, the font header is first downloaded.
  1867. The number of fonts stored in the printer is kept at |max_printer_fonts|
  1868. maximum. When more fonts are needed, the least recently used one is
  1869. deleted.
  1870. The value of |max_printer_fonts| was choosed by waving a magig rod;
  1871. it would be more appropriate to calculate exactly how many bytes of
  1872. the user-available memory in the LaserJet+ is in use, and base the
  1873. decisions whether we must delete some fonts on that.
  1874. (Why can't you ask the printer how much memory it has left?).
  1875. There should also be a test whether the number of fonts on a page
  1876. exceeds the maximum 16.
  1877. @<Finish a command that either sets or puts a character...@>=
  1878. if p<0 then p:=255-((-1-p) mod 256)
  1879. else if p>=256 then p:=p mod 256; {width computation for oriental fonts}
  1880. @^oriental characters@>@^Chinese characters@>@^Japanese characters@>
  1881. if (p>127) then q:=invalid_width
  1882. else q:=char_width(cur_font)(p);
  1883. if q=invalid_width then
  1884.   begin error('character ',p:1,' invalid in font ');
  1885. @.character $c$ invalid...@>
  1886.   print_font(cur_font);
  1887.   if cur_font<>nf then print('!'); {font |nf| has `\.!' in its name}
  1888.   end;
  1889. if font_used_on[cur_font]=0 then begin
  1890.   if fonts_in_use=max_printer_fonts then
  1891.     @<Delete least recently used font@>;
  1892.   incr(fonts_on_page);
  1893.   @<Download font header@> end
  1894. else if font_used_on[cur_font]<page_count then
  1895.   incr(fonts_on_page);
  1896. if fonts_on_page>max_fonts_on_page then
  1897.   error('too many fonts on this page');
  1898. font_used_on[cur_font]:=page_count;
  1899. if cur_font <> prev_font then
  1900.   write_lj(@=#27'('@>,font_num[cur_font]:1,'X');
  1901. prev_font:=cur_font;
  1902. if char_status(cur_font)(p) = too_large then
  1903.   raster_char(p)
  1904. else begin
  1905.   if char_status(cur_font)(p) = not_loaded then
  1906.     download_char(p);
  1907.   if char_status(cur_font)(p) > 0 then begin
  1908.     vv:=vv+char_status(cur_font)(p);
  1909.     update_pos;
  1910.     write_lj(vis_chr(p));
  1911.     vv:=vv-char_status(cur_font)(p); end
  1912.   else begin
  1913.     update_pos;
  1914.     write_lj(vis_chr(p));
  1915.   end;
  1916.   lj_h:=lj_h+char_pixel_width(cur_font)(p);
  1917. if o>=put1 then begin
  1918.   hh:=hh-char_pixel_width(cur_font)(p);
  1919.   goto done;
  1920. if q=invalid_width then q:=0
  1921. else hh:=hh+char_pixel_width(cur_font)(p);
  1922. goto move_right
  1923. @ @<Download font header@>=
  1924. begin
  1925.   height.i0:=4*round(font_design_size[cur_font]*
  1926.                      font_mag[cur_font]/1000.0*conv);
  1927.   if (height.i0 < 0) or (height.i0>10922) then
  1928.     height.i0:= 10922;
  1929.   pitch.i0:=height.i0-20; {is this value used for anything in the printer??}
  1930.   write_lj(@=#27'*c'@>,font_num[cur_font]:1,
  1931.   {char cell 255*255 pixels max}
  1932.   {baseline distance set to $255-$|baseline| }
  1933.     @='D'#27')s26W'#0#26#0#1#0#0#0@>, chr(255-baseline),
  1934.     @=#0#255#0#255#0#1#1#21@>,
  1935.     pitch.c[0], pitch.c[1], height.c[0], height.c[1],
  1936.     @=#0#0#0#0#0#0#27'*c4F'@>);
  1937.   incr(fonts_in_use);
  1938. @ @<Delete least recently...@>=
  1939. begin
  1940.   j:=9999;
  1941.   k:=nf;
  1942.   for i:=0 to nf-1 do
  1943.     if font_used_on[i] < j then begin
  1944.       j:=font_used_on[i];
  1945.       k:=i;
  1946.     end;
  1947.   write_lj(@=#27'*c'@>,font_num[k]:1,'d2F');
  1948.   font_used_on[k]:=0;
  1949.   for i:=0 to 127 do
  1950.     if (char_status(k)(i) > 0) or (char_status(k)(i) = loaded_ok) then
  1951.       char_status(k)(i):=not_loaded;
  1952.   decr(fonts_in_use);
  1953. @ @<Finish a command that either sets or puts a rule...@>=
  1954. q:=signed_quad;
  1955. qq:=rule_pixels(q);
  1956. pp:=rule_pixels(p);
  1957. if (p>0) and (q>0) then begin
  1958.   vv:=vv-pp;
  1959.   update_pos;
  1960.   write_lj(@=#27'*c'@>,qq:1,'a',pp:1,'b0P');
  1961.   vv:=vv+pp;
  1962. if o=put_rule then goto done;
  1963. hh:=hh+qq;
  1964. goto move_right
  1965. @ A sequence of consecutive rules, or consecutive characters in a fixed-width
  1966. font whose width is not an integer number of pixels, can cause |hh| to drift
  1967. far away from a correctly rounded value. \.{DVIplus} ensures that the
  1968. amount of drift will never exceed |max_drift| pixels.
  1969. Since \.{DVIplus} is intended to diagnose strange errors, it checks
  1970. carefully to make sure that |h| and |v| do not get out of range.
  1971. Normal \.{DVI}-reading programs need not do this.
  1972. @d infinity==@'17777777777 {$\infty$ (approximately)}
  1973. @d max_drift=2 {we insist that abs|(hh-pixel_round(h))<=max_drift|}
  1974. @<Finish a command that sets |h:=h+q|, then |goto done|@>=
  1975. if (h>0)and(q>0) then if h>infinity-q then
  1976.   begin error('arithmetic overflow! parameter changed from ',
  1977. @.arithmetic overflow...@>
  1978.     q:1,' to ',infinity-h:1);
  1979.   q:=infinity-h;
  1980.   end;
  1981. if (h<0)and(q<0) then if -h>q+infinity then
  1982.   begin error('arithmetic overflow! parameter changed from ',
  1983.     q:1, ' to ',(-h)-infinity:1);
  1984.   q:=(-h)-infinity;
  1985.   end;
  1986. hhh:=pixel_round(h+q);
  1987. if abs(hhh-hh)>max_drift then begin
  1988.   if hhh>hh then hh:=hhh-max_drift
  1989.   else hh:=hhh+max_drift;
  1990. h:=h+q;
  1991. if abs(h)>max_h_so_far then
  1992.   begin if abs(h)-round(h_offset/conv)>max_h+99 then
  1993.     begin error('warning: h > ',max_h:1,'!');
  1994. @.warning: |h|...@>
  1995.     max_h:=abs(h);
  1996.     end;
  1997.   max_h_so_far:=abs(h);
  1998.   end;
  1999. goto done
  2000. @ @<Finish a command that sets |v:=v+p|, then |goto done|@>=
  2001. if (v>0)and(p>0) then if v>infinity-p then
  2002.   begin error('arithmetic overflow! parameter changed from ',
  2003. @.arithmetic overflow...@>
  2004.     p:1,' to ',infinity-v:1);
  2005.   p:=infinity-v;
  2006.   end;
  2007. if (v<0)and(p<0) then if -v>p+infinity then
  2008.   begin error('arithmetic overflow! parameter changed from ',
  2009.     p:1, ' to ',(-v)-infinity:1);
  2010.   p:=(-v)-infinity;
  2011.   end;
  2012. vvv:=pixel_round(v+p);
  2013. if abs(vvv-vv)>max_drift then begin
  2014.   if vvv>vv then vv:=vvv-max_drift
  2015.   else vv:=vvv+max_drift;
  2016. v:=v+p;
  2017. if abs(v)>max_v_so_far then
  2018.   begin if abs(v)-round(v_offset/conv)>max_v+99 then
  2019.     begin error('warning: v > ',max_v:1,'!');
  2020. @.warning: |v|...@>
  2021.     max_v:=abs(v);
  2022.     end;
  2023.   max_v_so_far:=abs(v);
  2024.   end;
  2025. goto done
  2026. @ @<Finish a command that changes the current font...@>=
  2027. begin
  2028.   font_num[nf]:=p; cur_font:=0;
  2029.   while font_num[cur_font]<>p do incr(cur_font);
  2030.   goto done
  2031. @* Using the backpointers.
  2032. First comes a routine that illustrates how to find the postamble quickly.
  2033. @<Find the postamble, working back from the end@>=
  2034. n:=dvi_length;
  2035. if n<53 then bad_dvi('only ',n:1,' bytes long');
  2036. @.only n bytes long@>
  2037. m:=n-4;
  2038. repeat if m=0 then bad_dvi('all 223s');
  2039. @.all 223s@>
  2040. move_to_byte(m); k:=get_byte; decr(m);
  2041. until k<>223;
  2042. if k<>dvi_id then bad_dvi('ID byte is ',k:1);
  2043. @.ID byte is wrong@>
  2044. move_to_byte(m-3); q:=signed_quad;
  2045. if (q<0)or(q>m-33) then bad_dvi('post pointer ',q:1,' at byte ',m-3:1);
  2046. @.post pointer is wrong@>
  2047. move_to_byte(q); k:=get_byte;
  2048. if k<>post then bad_dvi('byte ',q:1,' is not post');
  2049. @.byte n is not post@>
  2050. post_loc:=q; first_backpointer:=signed_quad
  2051. @ Note that the last steps of the above code save the locations of the
  2052. the |post| byte and the final |bop|.  We had better declare these global
  2053. variables, together with another one that we will need shortly.
  2054. @<Glob...@>=
  2055. @!post_loc:integer; {byte location where the postamble begins}
  2056. @!first_backpointer:integer; {the pointer following |post|}
  2057. @!start_loc:integer; {byte location of the first page to process}
  2058. @!start_inx:integer; {index into |page_start| for first page}
  2059. @!last_loc:integer; {byte localtion of last page to process}
  2060. @!page_start:array[1..max_bops] of integer; {pointers to |bop|s}
  2061. @ The next routines follow the backpointers
  2062. to move through a \.{DVI} file in reverse order. \.{DVIplus} does this because
  2063. it wants to print the pages backwards as the LaserJet stacks
  2064. them with the printed side up.
  2065. First we search for the starting page and the last page.
  2066. @<Scan for page range to print@>=
  2067. q:=post_loc; p:=first_backpointer; start_loc:=-1;
  2068. repeat
  2069.   {now |q| points to a |post| or |bop| command; |p>=0| is prev pointer}
  2070.   if p>q-46 then
  2071.     bad_dvi('page link ',p:1,' after byte ',q:1);
  2072. @.page link wrong...@>
  2073.   q:=p; move_to_byte(q);
  2074.   k:=get_byte;
  2075.   if k=bop then incr(page_count)
  2076.   else bad_dvi('byte ',q:1,' is not bop');
  2077. @.byte n is not bop@>
  2078.   if page_count>max_bops then bad_dvi('there are too many pages');
  2079. @.there are too many pages@>
  2080.   page_start[page_count]:=q;
  2081.   for k:=0 to 9 do count[k]:=signed_quad;
  2082.   if start_match then begin start_loc:=q; start_inx:=page_count; end;
  2083.   p:=signed_quad;
  2084. until p<0;
  2085. if start_loc<0 then abort('starting page number could not be found!');
  2086. @.starting page number...@>
  2087. if (start_inx > max_pages) then
  2088.   last_loc:=page_start[start_inx-max_pages+1]
  2089.   last_loc:=page_start[1];
  2090. if page_count<>total_pages then
  2091.   print_ln('there are really ',page_count:1,
  2092.     ' pages, not ',total_pages:1,'!');
  2093. @.there are really n pages@>
  2094. @ This is the code that really goes through the pages to be printed
  2095. (in reverse order). It starts from the page pointed to by |last_loc| and
  2096. proceeds up to |start_loc|.
  2097. The code shown here uses a convention that has proved to be useful:
  2098. If the starting page was specified as, e.g., `\.{1.*.-5}', then
  2099. all page numbers in the file are displayed by showing the values of
  2100. counts 0, 1, and~2, separated by dots. Such numbers can, for example,
  2101. be displayed on the console of a printer when it is working on that
  2102. page.
  2103. @<Translate the page range in reverse order@>=
  2104. page_count:=0;
  2105. q:=last_loc;
  2106. repeat
  2107.   move_to_byte(q); k:=get_byte;
  2108.   incr(page_count);
  2109.   fonts_on_page:=0;
  2110.   for k:=0 to 9 do count[k]:=signed_quad;
  2111.   q:=signed_quad;
  2112.   print('[');
  2113.   for k:=0 to start_vals do
  2114.     begin print(count[k]:1);
  2115.       if k<start_vals then print('.');
  2116.     end;
  2117.   update_terminal;
  2118.   do_page;
  2119.   print('] ');
  2120.   update_terminal;
  2121. until q<start_loc;
  2122. @* Reading the postamble.
  2123. Now imagine that we are reading the \.{DVI} file and positioned just
  2124. four bytes after the |post| command. That, in fact, is the situation,
  2125. when the following part of \.{DVIplus} is called upon to read, translate,
  2126. and check the rest of the postamble.
  2127. @p procedure read_postamble;
  2128. var k:integer; {loop index}
  2129. @!p,@!q,@!m:integer; {general purpose registers}
  2130. begin post_loc:=cur_loc-5;
  2131. @.Postamble starts at byte n@>
  2132. if signed_quad<>numerator then
  2133.   print_ln('numerator doesn''t match the preamble!');
  2134. @.numerator doesn't match@>
  2135. if signed_quad<>denominator then
  2136.   print_ln('denominator doesn''t match the preamble!');
  2137. @.denominator doesn't match@>
  2138. if signed_quad<>mag then if new_mag=0 then
  2139.   print_ln('magnification doesn''t match the preamble!');
  2140. @.magnification doesn't match@>
  2141. max_v:=signed_quad; max_h:=signed_quad;@/
  2142. max_s:=get_two_bytes; total_pages:=get_two_bytes;@/
  2143. if total_pages>max_bops then
  2144.   bad_dvi('enormous number of pages (', total_pages:1, ')');
  2145. @.enormous number of pages@>
  2146. @<Process the font definitions of the postamble@>;
  2147. @<Make sure that the end of the file is well-formed@>;
  2148. @ No warning is given when |max_h_so_far| exceeds |max_h| by less than~100,
  2149. since 100 units is invisibly small; it's approximately the wavelength of
  2150. visible light, in the case of \TeX\ output. Rounding errors can be expected
  2151. to make |h| and |v| slightly more than |max_h| and |max_v|, every once in
  2152. a~while; hence small discrepancies are not cause for alarm.
  2153. @ When we get to the present code, the |post_post| command has
  2154. just been read.
  2155. @<Make sure that the end of the file is well-formed@>=
  2156. q:=signed_quad;
  2157. if q<>post_loc then
  2158.   print_ln('bad postamble pointer in byte ',cur_loc-4:1,'!');
  2159. @.bad postamble pointer@>
  2160. m:=get_byte;
  2161. if m<>dvi_id then print_ln('identification in byte ',cur_loc-1:1,
  2162. @.identification...should be n@>
  2163.     ' should be ',dvi_id:1,'!');
  2164. k:=cur_loc; m:=223;
  2165. while (m=223)and not eof_dvi_file do m:=get_byte;
  2166. if not eof_dvi_file then bad_dvi('signature in byte ',cur_loc-1:1,
  2167. @.signature...should be...@>
  2168.     ' should be 223')
  2169. else if cur_loc<k+4 then
  2170.   print_ln('not enough signature bytes at end of file (',
  2171. @.not enough signature bytes...@>
  2172.     cur_loc-k:1,')');
  2173. @ @<Process the font definitions...@>=
  2174. repeat k:=get_byte;
  2175. if (k>=fnt_def1)and(k<fnt_def1+4) then
  2176.   begin p:=first_par(k); define_font(p); print_nl; k:=nop;
  2177.   end;
  2178. until k<>nop;
  2179. if k<>post_post then
  2180.   print_ln('byte ',cur_loc-1:1,' is not postpost!')
  2181. @.byte n is not postpost@>
  2182. @* The main program.
  2183. Now we are ready to put it all together. This is where \.{DVIplus} starts,
  2184. and where it ends.
  2185. @p begin dont_catch_errors; initialize; {get all variables initialized}
  2186. dialog; {set up all the options}
  2187. @<Process the preamble@>;
  2188. @<Find the postamble, working back from the end@>;
  2189. read_postamble;
  2190. print_ln('Total of ', nf:1, ' fonts.');
  2191. @<Scan for page range to print@>;
  2192. write_lj(@=#27'E'#27'&l'@>, copies:1, @='X'#27'*t300R'@>);
  2193. @<Translate the page range...@>;
  2194. write_lj(@=#27'&l1X'@>);
  2195. write_ln(laser_file,'_');
  2196. print_nl
  2197. @ The main program needs a few global variables in order to do its work.
  2198. @<Glob...@>=
  2199. @!k,@!m,@!n,@!p,@!q:integer; {general purpose registers}
  2200. @ A \.{DVI}-reading program that reads the postamble first need not look at the
  2201. preamble; but \.{DVIplus} looks at the preamble in order to do error
  2202. checking, and to display the introductory comment.
  2203. @<Process the preamble@>=
  2204. if not open_dvi_file then
  2205.   bad_dvi('cannot open file');
  2206. @<Open and lock |laser_file|@>;
  2207. p:=get_byte; {fetch the first byte}
  2208. if p<>pre then bad_dvi('first byte isn''t start of preamble!');
  2209. @.First byte isn't...@>
  2210. p:=get_byte; {fetch the identification byte}
  2211. if p<>dvi_id then
  2212.   print_ln('identification in byte 1 should be ',dvi_id:1,'!');
  2213. @.identification...should be n@>
  2214. @<Compute the conversion factor@>;
  2215. p:=get_byte; {fetch the length of the introductory comment}
  2216. print('''');
  2217. while p>0 do
  2218.   begin decr(p); print(xchr[get_byte]);
  2219.   end;
  2220. print_ln('''')
  2221. @ The conversion factor |conv| is figured as follows: There are exactly
  2222. |n/d| \.{DVI} units per decimicron, and 254000 decimicrons per inch,
  2223. and |resolution| pixels per inch. Then we have to adjust this
  2224. by the stated amount of magnification.
  2225. @<Compute the conversion factor@>=
  2226. numerator:=signed_quad; denominator:=signed_quad;
  2227. if numerator<=0 then bad_dvi('numerator is ',numerator:1);
  2228. @.numerator is wrong@>
  2229. if denominator<=0 then bad_dvi('denominator is ',denominator:1);
  2230. @.denominator is wrong@>
  2231. conv:=(numerator/254000.0)*(resolution/denominator);
  2232. mag:=signed_quad;
  2233. if new_mag>0 then mag:=new_mag
  2234. else if mag<=0 then bad_dvi('magnification is ',mag:1);
  2235. @.magnification is wrong@>
  2236. true_conv:=conv; conv:=true_conv*(mag/1000.0);
  2237. @* System-dependent changes.
  2238. This section sho
  2239.